waLBerlaHelperFunctions.cmake 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10

#######################################################################################################################
#
# Function to add a flag to the given flag string
#
# param _VAR:  flag string
# param _FLAG: flag to add
# param ARGV0: optional boolean value if the flag should be added
#
#######################################################################################################################
11
function ( add_flag  _VAR  _FLAG )
12
13
14
   if ( ARGC EQUAL 0 OR ARGV0 )
      set ( ${_VAR} "${${_VAR}} ${_FLAG}" PARENT_SCOPE )
   endif ( )
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
endfunction ( add_flag )
#######################################################################################################################



#######################################################################################################################
#
# Function to handle source files of type .gen.py and .gen.cuda.py
#
# files .gen.py generate a .h and a .cpp file
# files .gen.cuda.py generate a .h and a .cu file
# Takes a list of source files that contain .py files, and returns a list of source files
# where the generated version are added and the .py files are removed.
# Additionally creates a custom build rule for the code generation
#
#######################################################################################################################
31
function( handle_python_codegen sourceFilesOut generatedSourceFilesOut generatorsOut codeGenRequiredOut )
32
    set(result )
33
34
    set(generatedResult )
    set(generatorsResult )
35
36
37
    set(codeGenRequired NO)
    foreach( sourceFile ${ARGN} )
        if( ${sourceFile} MATCHES ".*\\.gen\\.py$" )
38
39
40
41
            get_filename_component(sourceFileName ${sourceFile} NAME)
            if( ${sourceFileName} MATCHES ".*\\.cuda\\.gen\\.py$" )
                string(REPLACE ".cuda.gen.py" ".h"  genHeaderFile ${sourceFileName})
                string(REPLACE ".cuda.gen.py" ".cu" genSourceFile ${sourceFileName})
42
            else()
43
44
                string(REPLACE ".gen.py" ".h"  genHeaderFile ${sourceFileName})
                string(REPLACE ".gen.py" ".cpp" genSourceFile ${sourceFileName})
45
            endif()
46
47
            list(APPEND generatedResult ${CMAKE_CURRENT_BINARY_DIR}/${genSourceFile}
                                        ${CMAKE_CURRENT_BINARY_DIR}/${genHeaderFile})
48
49
            add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${genSourceFile}
                                      ${CMAKE_CURRENT_BINARY_DIR}/${genHeaderFile}
50
51
                               DEPENDS ${sourceFile}
                               COMMAND ${PYTHON_EXECUTABLE} ${sourceFile}
52
53
                               WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
            include_directories(${CMAKE_CURRENT_BINARY_DIR})
54
            list(APPEND generatorsResult ${sourceFile} )
55
56
57
58
59
60
            set(codeGenRequired YES)
        else()
            list(APPEND result ${sourceFile})
        endif()
    endforeach()
    set( ${sourceFilesOut} ${result} PARENT_SCOPE )
61
62
    set( ${generatedSourceFilesOut} ${generatedResult} PARENT_SCOPE )
    set( ${generatorsOut} ${generatorsResult} PARENT_SCOPE )
63
64
    set( ${codeGenRequiredOut} ${codeGenRequired} PARENT_SCOPE )
endfunction ( handle_python_codegen )
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#######################################################################################################################




#######################################################################################################################
#
# Subtracts list2 from list 2
# 
# Keywords:
#     LIST1   first list
#     LIST2   second list
# Example:
#     list_minus ( result LIST1 "entry1" "entry2" "entry3" LIST2 "entry1" "entry3" )
#      -> result has a single entry: "entry2"
#     
#######################################################################################################################
function( list_minus resultOut )
    set( options )
    set( oneValueArgs )
    set( multiValueArgs LIST1 LIST2 )
    cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
   
    set ( result ${ARG_LIST1} )
    foreach ( secondEntry ${ARG_LIST2} )
        list (REMOVE_ITEM result ${secondEntry} )
    endforeach()
    
    set ( ${resultOut} ${result} PARENT_SCOPE )
    
endfunction( list_minus )    
#######################################################################################################################




#######################################################################################################################
#
# Adds waLBerla to cmake package registry, and sets internal cache variables 
# ( include paths, compile definitions, compiler flags ... ) 
# 
# Which can be read by projects that add waLBerla as a subdirectory
# See for example waLBerla_import 
#    
#######################################################################################################################
function ( waLBerla_export )
    # Write the build directory to the cmake package config 
    # which is under  ~/.cmake/packages (Linux), or in the registry (Windows) 
    export( PACKAGE waLBerla )    
    
    # Copy a config file to the build directory which is executed when from another project
    # find_package( waLBerla ) is called 
    configure_file ( cmake/walberla-config-builddir.cmake
                     walberla-config.cmake @ONLY )
    
    # Export compiler flags
    set ( compilerList "CXX" "C" )
    foreach ( compiler ${compilerList} )
        set ( WALBERLA_INCLUDE_SYSTEM_FLAG_${compiler}  ${CMAKE_INCLUDE_SYSTEM_FLAG_${compiler}}
              CACHE INTERNAL "${compiler} include flags for walberla" )
        set ( WALBERLA_${compiler}_FLAGS ${CMAKE_${compiler}_FLAGS} 
              CACHE INTERNAL "${compiler} flags for walberla" )
              
        foreach ( buildType ${CMAKE_CONFIGURATION_TYPES} )        
            set ( WALBERLA_${compiler}_FLAGS_${buildType} ${CMAKE_${compiler}_FLAGS_${buildType}} 
                  CACHE INTERNAL "${compiler} flags for walberla" )
        endforeach()
    endforeach()    
    
    # Export linker flags
    set ( WALBERLA_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} CACHE INTERNAL "walberla linker flags")
    foreach ( buildType ${CMAKE_CONFIGURATION_TYPES} )        
        set ( WALBERLA_EXE_LINKER_FLAGS_${buildType} ${CMAKE_EXE_LINKER_FLAGS_${buildType}} 
                CACHE INTERNAL "walberla linker flags for ${buildType}" )
    endforeach()
    
    # Export service libs
    set ( WALBERLA_SERVICE_LIBS ${SERVICE_LIBS} CACHE INTERNAL "External Libraries necessary for waLBerla" )
    
    # Export compile definitions
    get_directory_property( WALBERLA_COMPILE_DEFINITIONS DIRECTORY ${walberla_SOURCE_DIR} COMPILE_DEFINITIONS )
    set ( WALBERLA_COMPILE_DEFINITIONS ${WALBERLA_COMPILE_DEFINITIONS} CACHE INTERNAL "waLBerla compile definitions" )

    # Export include paths
    get_directory_property( WALBERLA_INCLUDE_DIRS DIRECTORY ${walberla_SOURCE_DIR} INCLUDE_DIRECTORIES)
    set ( WALBERLA_INCLUDE_DIRS ${WALBERLA_INCLUDE_DIRS} CACHE INTERNAL "waLBerla include directories" )
    
    # Export link paths
    set ( WALBERLA_LINK_DIRS ${LINK_DIRS} CACHE INTERNAL "waLBerla link directories" )
endfunction( waLBerla_export)

#######################################################################################################################




#######################################################################################################################
#
# Reads variables that have been exported by waLBerla_export
# Useful when waLBerla was added as a subdirectory
#
#######################################################################################################################
function ( waLBerla_import )
    set( options NO_COMPILER_FLAGS NO_INCLUDE_DIRS NO_COMPILE_DEFINITIONS )
    set( oneValueArgs )
    set( multiValueArgs  )
    cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

    if ( NOT NO_INCLUDE_DIRS )
        foreach( directory ${WALBERLA_INCLUDE_DIRS} )
            if( ${directory} MATCHES "walberla/src$" )
                include_directories( ${directory} )
            else()
                include_directories( SYSTEM ${directory} )
            endif()
        endforeach()
    endif()
    
    # Import compile definitions
    if ( NOT NO_COMPILE_DEFINITIONS )
        set_property( DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY COMPILE_DEFINITIONS ${WALBERLA_COMPILE_DEFINITIONS} )
    endif()
        
    # Import compiler flags
    if ( NOT NO_COMPILER_FLAGS )
        set ( compilerList "CXX" "C" )
        foreach ( compiler ${compilerList} )
            set ( CMAKE_INCLUDE_SYSTEM_FLAG_${compiler}  ${WALBERLA_INCLUDE_SYSTEM_FLAG_${compiler}}  PARENT_SCOPE )
            set ( CMAKE_${compiler}_FLAGS                ${WALBERLA_${compiler}_FLAGS}                PARENT_SCOPE )
                  
            foreach ( buildType ${CMAKE_CONFIGURATION_TYPES} )        
                set ( CMAKE_${compiler}_FLAGS_${buildType} ${WALBERLA_${compiler}_FLAGS_${buildType}} PARENT_SCOPE )
            endforeach()
        endforeach()    
        
        # Import linker flags
        set ( CMAKE_EXE_LINKER_FLAGS ${WALBERLA_EXE_LINKER_FLAGS}  )
        foreach ( buildType ${CMAKE_CONFIGURATION_TYPES} )        
            set ( CMAKE_EXE_LINKER_FLAGS_${buildType} ${WALBERLA_EXE_LINKER_FLAGS_${buildType}} PARENT_SCOPE )
        endforeach() 
    endif()
    
    set( SERVICE_LIBS ${WALBERLA_SERVICE_LIBS} PARENT_SCOPE )
    
    link_directories( ${WALBERLA_LINK_DIRS} )
endfunction( waLBerla_import)
#######################################################################################################################


#######################################################################################################################
#
# Group Files for an IDE like VS
# 
# Keywords:
#     FILES     all files which are in a certain group
# Example:
#     file        ( GLOB_RECURSE sourceFiles "*.h" "*.c" )
#     group_files ( "Source Files" FILES ${sourceFiles} )
#     
#######################################################################################################################
function( group_files group )
    set( options )
    set( oneValueArgs)
    set( multiValueArgs FILES)
    cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
    
    foreach( filename ${ARG_FILES} )
       file( RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${filename})
       if( rel MATCHES ".*/.*" )
          # subdirectory found -> add to subdirectory
          get_filename_component(dir ${rel} PATH )
          # replace slashes by some underscores to use path as variable name 
          # -> you are allowed to use at max two underscores in your folder name
          string( REGEX REPLACE  "/" "___" subname ${dir} )
          list( APPEND subfiles${subname} ${filename} )
          list( APPEND subnames           ${subname}  )
       else()
          # no subdirectory found -> add to normal group
          list( APPEND files ${filename} )
       endif()
    endforeach()
    
    if( subnames )
       list( REMOVE_DUPLICATES subnames )
    endif()
    
    foreach( subname ${subnames} )
       string( REGEX REPLACE  "___" "\\\\" subgroup ${subname} )
       #message( STATUS " Group: ${group}\\${subgroup} and files: ${subfiles${subname}}" )
       source_group( "${group}\\${subgroup}" FILES ${subfiles${subname}} )
    endforeach()
    #message( STATUS " Group: ${group} and files: ${files}" )
    source_group( "${group}" FILES ${files} )
endfunction( group_files )
#######################################################################################################################



#######################################################################################################################
#
# Sets version number of waLberla
# 
# Example:
#     
#######################################################################################################################
function( set_version VERSION_MAJOR VERSION_PATCH )
  set( WALBERLA_MAJOR_VERSION ${VERSION_MAJOR} CACHE STRING "waLBerla major version" FORCE )
  set( WALBERLA_PATCH_LEVEL   ${VERSION_PATCH} CACHE STRING "waLBerla patch level" FORCE )
  set( WALBERLA_VERSION "${VERSION_MAJOR}.${VERSION_PATCH}" CACHE STRING "waLBerla version" FORCE )
  mark_as_advanced( WALBERLA_MAJOR_VERSION WALBERLA_PATCH_LEVEL )
endfunction()
#######################################################################################################################