1 # - Wmake compatibility module
3 # FOAM_ADD_WMAKE_TARGET([<target-var> [<dir>]])
5 # Create a target that is described by the OpenFOAM wmake build system. The
6 # function parses the files <dir>/Make/files and <dir>/Make/options and then
7 # creates an executable or a library. The name of the target will be returned
8 # in <target-var> and <dir> defaults to CMAKE_CURRENT_SOURCE_DIR. If the target
9 # name ends on "Foam" (but not "ToFoam"), this suffix will be stripped. The
10 # parsing is quite rudimentary, especially variable substitution might fail and
11 # the include-directories detection is not very robust at the moment. Consider
12 # this function to be very experimental. Comments and patches are welcome.
15 #-------------------------------------------------------------------------------
17 # | ____| _| |_ / __ \ /\ | \/ |
18 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
19 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
20 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
21 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
23 # FreeFOAM: The Cross-Platform CFD Toolkit
25 # Copyright (C) 2008-2011 Michael Wild <themiwi@users.sf.net>
26 # Gerber van der Graaf <gerber_graaf@users.sf.net>
27 #-------------------------------------------------------------------------------
29 # This file is part of FreeFOAM.
31 # FreeFOAM is free software; you can redistribute it and/or modify it
32 # under the terms of the GNU General Public License as published by the
33 # Free Software Foundation; either version 2 of the License, or (at your
34 # option) any later version.
36 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
37 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
38 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
41 # You should have received a copy of the GNU General Public License
42 # along with FreeFOAM; if not, write to the Free Software Foundation,
43 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
46 # Wmake compatibility module
48 #-------------------------------------------------------------------------------
51 find_program(CPP_EXECUTABLE cpp)
52 mark_as_advanced(CPP_EXECUTABLE)
53 if(NOT CPP_EXECUTABLE)
54 message(SEND_ERROR "Required program 'cpp' not found")
58 set(proptype_tmpl "DIRECTORY;\${dir}")
63 # Run <filename> through the preprocessor and return
64 # the results in <var>.
65 function(_foam_awt_preprocess var filename)
67 if(FOAM_DOUBLE_PRECISION OR FOAM_DEFINITIONS MATCHES -DDP)
68 list(APPEND defs -DDP)
70 list(APPEND defs -DSP)
73 COMMAND "${CPP_EXECUTABLE}" -xc ${defs} -P -E ${filename}
79 message(SEND_ERROR "Preprocessing of ${filename} failed with:\n${err}")
81 # wrap line continuations
82 string(REGEX REPLACE "\\\\n" " " out "${out}")
83 # strip leading/trailing space and empty lines
84 string(STRIP "${out}" out)
85 string(REGEX REPLACE "\n\n+" "\n" out "${out}")
86 set(${var} "${out}" PARENT_SCOPE)
89 # Parse the variable definitions in <str> and store the names in
90 # the FOAM_AWT_VARIABLES property of <dir> and the values in
91 # FOAM_AWT_VALUE_<varname>. The definitions are removed from the
92 # string and the result is returned in <outvar>
93 function(_foam_awt_collect_vars outvar dir str)
95 string(CONFIGURE "${proptype_tmpl}" proptype)
97 # initialize well known variables
98 foreach(var FOAM_APPBIN FOAM_LIBBIN FOAM_SOLVERS FOAM_SRC FOAM_USER_APPBIN
99 FOAM_USER_LIBBIN LIB_SRC)
100 set_property(${proptype} APPEND PROPERTY FOAM_AWT_VARIABLES ${var})
101 set_property(${proptype} PROPERTY FOAM_AWT_VALUE_${var} "${var}")
103 string(REPLACE "\n" ";" lines "${str}")
105 if(l MATCHES "^[ \t]*([^ \t]+)[ \t]*=[ \t]*([^ \t].*)")
106 set_property(${proptype} APPEND PROPERTY FOAM_AWT_VARIABLES ${CMAKE_MATCH_1})
107 string(STRIP "${CMAKE_MATCH_2}" val)
108 set_property(${proptype} PROPERTY FOAM_AWT_VALUE_${CMAKE_MATCH_1} "${val}")
109 list(REMOVE_ITEM lines "${l}")
112 string(REPLACE ";" "\n" lines "${lines}")
113 set(${outvar} "${lines}" PARENT_SCOPE)
116 # Convenience function to retrieve the value of the FOAM_AWT_VALUE_<name>
117 # property of <dir> in <outvar>.
118 function(_foam_awt_get_value outvar dir name)
120 string(CONFIGURE "${proptype_tmpl}" proptype)
122 get_property(variables ${proptype} PROPERTY FOAM_AWT_VARIABLES)
123 list(FIND variables ${name} idx)
125 message(FATAL_ERROR "Failed to resolve variable ${name}")
127 get_property(val ${proptype} PROPERTY FOAM_AWT_VALUE_${name})
128 set(${outvar} "${val}" PARENT_SCOPE)
131 # Try to resolve all $(name) variable references in <str> using the variables
132 # stored in the directory properties of <dir> and return the resulting string
134 function(_foam_awt_resolve_vars outvar dir str)
136 string(CONFIGURE "${proptype_tmpl}" proptype)
139 get_property(variables ${proptype} PROPERTY FOAM_AWT_VARIABLES)
140 foreach(var ${variables})
141 get_property(val ${proptype} PROPERTY FOAM_AWT_VALUE_${var})
142 while(val MATCHES "\\$\\(([^ \t)]+)\\)")
143 set(ref ${CMAKE_MATCH_1})
144 _foam_awt_get_value(refval "${dir}" ${ref})
145 string(REGEX REPLACE "\\$\\(${ref}\\)" "${refval}" val "${val}")
147 set_property(${proptype} PROPERTY FOAM_AWT_VALUE_${var} "${val}")
150 while(str MATCHES "\\$\\(([^ \t)]+)\\)")
151 set(ref ${CMAKE_MATCH_1})
152 _foam_awt_get_value(refval "${dir}" ${ref})
153 string(REGEX REPLACE "\\$\\(${ref}\\)" "${refval}" str "${str}")
155 set(${outvar} "${str}" PARENT_SCOPE)
158 # Determines whether <lib> is a library built by @PROJECT_NAME@
159 function(_foam_awt_is_foam_library outvar lib)
162 if(TARGET FOAM_${lib})
167 "@FOAM_LIB_DIR@/@CMAKE_SHARED_LIBRARY_PREFIX@${lib}@CMAKE_SHARED_LIBRARY_SUFFIX@"
169 "@FOAM_LIB_DIR@/@CMAKE_SHARED_LIBRARY_PREFIX@${lib}@CMAKE_SHARED_MODULE_SUFFIX@"
174 set(${outvar} ${result} PARENT_SCOPE)
177 # Parse <dir>/Make/files and <dir>/Make/options and return the target type in
178 # <typevar> (either LIB or EXE if it is a library or executable, respectively),
179 # the target name in <targetvar>, the list of source files in <filesvar>, the
180 # link libraries in <libsvar> and the include directories in <incdirsvar>.
181 function(_foam_awt_parse typevar targetvar filesvar libsvar incdirsvar dir)
183 string(CONFIGURE "${proptype_tmpl}" proptype)
185 set(f_files "${dir}/Make/files")
186 set(f_options "${dir}/Make/options")
187 foreach(f f_files f_options)
188 if(NOT EXISTS "${${f}}")
189 message(SEND_ERROR "The file '${${f}}' does not exist")
192 _foam_awt_preprocess(l_files "${f_files}")
193 _foam_awt_preprocess(l_options "${f_options}")
194 set(lines "${l_files}\n${l_options}")
195 _foam_awt_collect_vars(lines "${dir}" "${lines}")
196 _foam_awt_resolve_vars(lines "${dir}" "${lines}")
197 string(REPLACE "\n" ";" lines "${lines}")
198 set(files ${f_files} ${f_options})
200 set_source_files_properties(${files} PROPERTIES HEADER_FILE_ONLY TRUE)
203 string(STRIP "${f}" f)
204 # this presumably is a file
205 if(NOT IS_ABSOLUTE ${f})
209 list(APPEND files "${f}")
211 message("WARNING: No such file '${f}'")
214 get_property(lib_name ${proptype} PROPERTY FOAM_AWT_VALUE_LIB)
215 get_property(exe_name ${proptype} PROPERTY FOAM_AWT_VALUE_EXE)
218 set(target ${exe_name})
221 set(target ${lib_name})
223 message(FATAL_ERROR "ERROR: '${f_files}'\n"
224 "does neither contain a definition for EXE or LIB")
226 get_filename_component(target "${target}" NAME_WE)
228 get_property(libs_str ${proptype} PROPERTY FOAM_AWT_VALUE_${type}_LIBS)
229 separate_arguments(libs_str)
231 foreach(l ${libs_str})
232 string(STRIP "${l}" l)
233 string(REGEX REPLACE "^-l" "" l "${l}")
234 _foam_awt_is_foam_library(is_foam_lib ${l})
238 list(APPEND libs ${l})
240 # get include directories
241 get_property(incdirs_str ${proptype} PROPERTY FOAM_AWT_VALUE_EXE_INC)
242 separate_arguments(incdirs_str)
244 foreach(dd ${incdirs_str})
246 if(d MATCHES "lnInclude$")
247 get_filename_component(d "${d}" PATH)
249 get_filename_component(d "${d}" NAME)
250 set(d "@FOAM_INCLUDE_DIR@/${d}")
251 if(IS_DIRECTORY "${d}")
252 list(APPEND incdirs "${d}")
254 message("WARNING: Don't know how to translate include-directory '${dd}'")
257 # return arguments to caller
258 set(${typevar} ${type} PARENT_SCOPE)
259 set(${targetvar} ${target} PARENT_SCOPE)
260 set(${filesvar} "${files}" PARENT_SCOPE)
261 set(${libsvar} "${libs}" PARENT_SCOPE)
262 set(${incdirsvar} "${incdirs}" PARENT_SCOPE)
265 function(foam_add_wmake_target)
274 set(dir "${CMAKE_CURRENT_SOURCE_DIR}")
276 _foam_awt_parse(type target files libs incdirs "${dir}")
278 include_directories(${incdirs})
280 if(type STREQUAL EXE)
281 # try to be smart about the executable name
282 if(NOT target MATCHES "ToFoam$"AND target MATCHES "(.*)Foam$" )
283 set(target ${CMAKE_MATCH_1})
285 foam_add_executable(${target} ${files})
286 elseif(type STREQUAL LIB)
287 foam_add_library(${target} ${files})
289 target_link_libraries(${target} ${libs})
291 set(${outvar} ${target} PARENT_SCOPE)
295 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file