BuildFunctions.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. ################################################################################
  2. #
  3. # macros and functions
  4. #
  5. ################################################################################
  6. ########################################
  7. # FUNCTION set_output_directory
  8. ########################################
  9. function(set_output_directory target dir)
  10. set(out ${output_dir})
  11. if (MSVC OR XCODE) # multiconfiguration builds
  12. set(out ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  13. endif()
  14. if ("${ARGV2}" STREQUAL "CURRENT_DIR")
  15. set(out .)
  16. if (UNIX AND "${dir}" STREQUAL ".")
  17. set(dir bin)
  18. endif()
  19. endif()
  20. if (MSVC OR XCODE)
  21. foreach(conf ${CMAKE_CONFIGURATION_TYPES})
  22. string(TOUPPER ${conf} conf2)
  23. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${conf2} ${out}/${conf}/${dir})
  24. set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${conf2} ${out}/${conf}/${dir})
  25. endforeach()
  26. else() # single configuration
  27. add_custom_command(TARGET ${target} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${out}/${dir})
  28. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${out}/${dir})
  29. set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${out}/${dir})
  30. endif()
  31. endfunction(set_output_directory)
  32. ########################################
  33. # FUNCTION set_output_directory_unix
  34. ########################################
  35. function(set_output_directory_unix target dir)
  36. if (UNIX)
  37. set_output_directory(${target} ${dir} ${ARGN})
  38. endif()
  39. endfunction(set_output_directory_unix)
  40. ########################################
  41. # FUNCTION set_exported_symbols
  42. ########################################
  43. if (WIN32)
  44. function(set_exported_symbols target filename)
  45. set(def_file ${filename}.def)
  46. if ("${filename}" STREQUAL "empty")
  47. set(def_file)
  48. elseif("${filename}" STREQUAL "fbplugin")
  49. set(def_file "plugin.def")
  50. endif()
  51. if (NOT "${def_file}" STREQUAL "")
  52. if (MSVC)
  53. set_target_properties(${target} PROPERTIES LINK_FLAGS "/DEF:\"${CMAKE_SOURCE_DIR}/builds/win32/defs/${def_file}\"")
  54. endif()
  55. if (MINGW)
  56. #set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,${CMAKE_SOURCE_DIR}/builds/win32/defs/${def_file}")
  57. endif()
  58. endif()
  59. endfunction(set_exported_symbols)
  60. endif()
  61. if (UNIX)
  62. function(set_exported_symbols target filename)
  63. set(def_file ${filename}.vers)
  64. if ("${filename}" STREQUAL "ib_udf")
  65. set(def_file)
  66. endif()
  67. if (NOT "${def_file}" STREQUAL "")
  68. set(wl_option "--version-script")
  69. if (APPLE)
  70. set(wl_option "-exported_symbols_list")
  71. endif()
  72. set_target_properties(${target} PROPERTIES LINK_FLAGS -Wl,${wl_option},${CMAKE_BINARY_DIR}/builds/posix/${def_file})
  73. endif()
  74. endfunction(set_exported_symbols)
  75. endif(UNIX)
  76. ########################################
  77. # FUNCTION epp_process
  78. ########################################
  79. function(epp_process type files)
  80. set(epp_suffix ".${type}.cpp")
  81. foreach(F ${${files}})
  82. set(in ${CMAKE_CURRENT_SOURCE_DIR}/${F})
  83. set(out ${CMAKE_CURRENT_BINARY_DIR}/${F}${epp_suffix})
  84. get_filename_component(dir ${out} PATH)
  85. if (MSVC OR XCODE)
  86. set(dir ${dir}/$<CONFIG>)
  87. endif()
  88. if ("${type}" STREQUAL "boot")
  89. add_custom_command(
  90. OUTPUT ${out}
  91. DEPENDS gpre_boot ${in}
  92. COMMENT "Calling GPRE boot for ${F}"
  93. #
  94. COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
  95. COMMAND ${ARGN} ${in} ${out}
  96. )
  97. elseif ("${type}" STREQUAL "master")
  98. get_filename_component(file ${out} NAME)
  99. set(dir ${dir}/${file}.d)
  100. add_custom_command(
  101. OUTPUT ${out}
  102. DEPENDS databases boot_gpre ${in}
  103. COMMENT "Calling GPRE master for ${F}"
  104. #
  105. COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
  106. COMMAND ${CMAKE_COMMAND} -E copy_if_different metadata.fdb ${dir}/yachts.lnk
  107. COMMAND ${CMAKE_COMMAND} -E copy_if_different security.fdb ${dir}/security.fdb
  108. COMMAND ${CMAKE_COMMAND} -E copy_if_different msg.fdb ${dir}/msg.fdb
  109. COMMAND ${CMAKE_COMMAND} -E copy_if_different help.fdb ${dir}/help.fdb
  110. COMMAND ${ARGN} -b ${dir}/ ${in} ${out}
  111. )
  112. endif()
  113. endforeach()
  114. endfunction(epp_process)
  115. ########################################
  116. # FUNCTION add_epp_suffix
  117. ########################################
  118. function(add_epp_suffix files suffix)
  119. foreach(F ${${files}})
  120. list(APPEND ${files}_${suffix} ${CMAKE_CURRENT_SOURCE_DIR}/${F})
  121. list(APPEND ${files}_${suffix} ${GENERATED_DIR}/${F}.${suffix}.cpp)
  122. endforeach()
  123. set_source_files_properties(${${files}_${suffix}} PROPERTIES GENERATED TRUE)
  124. set(${files}_${suffix} ${${files}_${suffix}} PARENT_SCOPE)
  125. endfunction(add_epp_suffix)
  126. ########################################
  127. # FUNCTION set_win32
  128. ########################################
  129. function(set_win32 var)
  130. if (WIN32)
  131. set(${var} "${ARGN}" PARENT_SCOPE)
  132. endif()
  133. endfunction(set_win32)
  134. ########################################
  135. # FUNCTION set_unix
  136. ########################################
  137. function(set_unix var)
  138. if (UNIX)
  139. set(${var} "${ARGN}" PARENT_SCOPE)
  140. endif()
  141. endfunction(set_unix)
  142. ########################################
  143. # FUNCTION set_apple
  144. ########################################
  145. function(set_apple var)
  146. if (APPLE)
  147. set(${var} "${ARGN}" PARENT_SCOPE)
  148. endif()
  149. endfunction(set_apple)
  150. ########################################
  151. # FUNCTION add_src_win32
  152. ########################################
  153. function(add_src_win32 var)
  154. if (WIN32)
  155. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  156. endif()
  157. endfunction(add_src_win32)
  158. ########################################
  159. # FUNCTION add_src_unix
  160. ########################################
  161. function(add_src_unix var)
  162. if (UNIX)
  163. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  164. endif()
  165. endfunction(add_src_unix)
  166. ########################################
  167. # FUNCTION add_src_unix_not_apple
  168. ########################################
  169. function(add_src_unix_not_apple var)
  170. if (UNIX AND NOT APPLE)
  171. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  172. endif()
  173. endfunction(add_src_unix_not_apple)
  174. ########################################
  175. # FUNCTION add_src_apple
  176. ########################################
  177. function(add_src_apple var)
  178. if (APPLE)
  179. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  180. endif()
  181. endfunction(add_src_apple)
  182. ########################################
  183. # FUNCTION copy_and_rename_lib
  184. ########################################
  185. function(copy_and_rename_lib target name)
  186. set(name2 $<TARGET_FILE_DIR:${target}>/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX})
  187. add_custom_command(
  188. TARGET ${target}
  189. POST_BUILD
  190. COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${target}> ${name2}
  191. )
  192. endfunction(copy_and_rename_lib)
  193. ########################################
  194. # FUNCTION project_group
  195. ########################################
  196. function(project_group target name)
  197. set_target_properties(${target} PROPERTIES FOLDER ${name})
  198. endfunction(project_group)
  199. ########################################
  200. # FUNCTION set_generated_directory
  201. ########################################
  202. function(set_generated_directory)
  203. if (NOT CMAKE_CROSSCOMPILING)
  204. set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
  205. else()
  206. string(REPLACE "${CMAKE_BINARY_DIR}" "${NATIVE_BUILD_DIR}" GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR})
  207. set(GENERATED_DIR ${GENERATED_DIR} PARENT_SCOPE)
  208. endif()
  209. endfunction(set_generated_directory)
  210. ########################################
  211. # FUNCTION add_dependencies_cc (cross compile)
  212. ########################################
  213. function(add_dependencies_cc target)
  214. if (NOT CMAKE_CROSSCOMPILING)
  215. add_dependencies(${target} ${ARGN})
  216. endif()
  217. endfunction(add_dependencies_cc)
  218. ########################################
  219. # FUNCTION add_dependencies_unix_cc (cross compile)
  220. ########################################
  221. function(add_dependencies_unix_cc target)
  222. if (UNIX)
  223. add_dependencies_cc(${target} ${ARGN})
  224. endif()
  225. endfunction(add_dependencies_unix_cc)
  226. ########################################
  227. # FUNCTION crosscompile_prebuild_steps
  228. ########################################
  229. function(crosscompile_prebuild_steps)
  230. if (CMAKE_CROSSCOMPILING)
  231. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NATIVE_BUILD_DIR}/src/include/gen/parse.h ${CMAKE_BINARY_DIR}/src/include/gen/parse.h)
  232. endif()
  233. endfunction(crosscompile_prebuild_steps)
  234. ########################################
  235. # FUNCTION create_command
  236. ########################################
  237. function(create_command command type out)
  238. set(dir ${output_dir})
  239. if ("${type}" STREQUAL "boot")
  240. set(dir ${boot_dir})
  241. endif()
  242. set_win32(env "PATH=${dir}\;%PATH%")
  243. set_unix (env "PATH=${dir}/bin:$PATH")
  244. set(env "${env}"
  245. FIREBIRD=${dir}
  246. )
  247. set(cmd_name ${command})
  248. if (MSVC OR XCODE)
  249. set(conf _$<CONFIG>)
  250. endif()
  251. set(pre_cmd)
  252. set(ext .sh)
  253. set(export export)
  254. set(options $*)
  255. set(perm)
  256. if (WIN32)
  257. set(pre_cmd @)
  258. set(ext .bat)
  259. set(export set)
  260. set(options %*)
  261. endif()
  262. set(cmd_name ${cmd_name}${conf}${ext})
  263. set(cmd_name ${CMAKE_BINARY_DIR}/src/${cmd_name})
  264. set(content)
  265. foreach(e ${env})
  266. set(content "${content}${pre_cmd}${export} ${e}\n")
  267. endforeach()
  268. set(cmd $<TARGET_FILE:${cmd}>)
  269. set(content "${content}${pre_cmd}${cmd} ${options}")
  270. file(GENERATE OUTPUT ${cmd_name} CONTENT "${content}")
  271. if (UNIX)
  272. set(cmd_name chmod u+x ${cmd_name} COMMAND ${cmd_name})
  273. endif()
  274. string(TOUPPER ${command} CMD)
  275. set(${CMD}_CMD ${cmd_name} PARENT_SCOPE)
  276. set(${out} ${CMD}_CMD PARENT_SCOPE)
  277. endfunction(create_command)
  278. ########################################
  279. # FUNCTION create_boot_commands
  280. ########################################
  281. function(create_boot_commands)
  282. set(cmd_list
  283. boot_isql
  284. boot_gpre
  285. boot_gbak
  286. boot_gfix
  287. build_msg
  288. codes
  289. gpre_boot
  290. )
  291. foreach(cmd ${cmd_list})
  292. create_command(${cmd} boot out)
  293. set(${out} ${${out}} PARENT_SCOPE)
  294. endforeach()
  295. endfunction(create_boot_commands)
  296. ########################################
  297. # FUNCTION create_master_commands
  298. ########################################
  299. function(create_master_commands)
  300. set(cmd_list
  301. isql
  302. gpre
  303. empbuild
  304. )
  305. foreach(cmd ${cmd_list})
  306. create_command(${cmd} master out)
  307. set(${out} ${${out}} PARENT_SCOPE)
  308. endforeach()
  309. endfunction(create_master_commands)
  310. ################################################################################