VleUtilsConfig.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. # VleCheckDependencies.cmake
  2. # ===========================
  3. #
  4. # Check the dependencies of the current package
  5. #
  6. # Copyright (c) 2014-2014 INRA
  7. # Ronan Trépos <rtrepos@toulouse.inra.fr>
  8. #
  9. # Distributed under the OS-approved BSD License (the "License");
  10. # This software is distributed WITHOUT ANY WARRANTY; without even the
  11. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. # See the License for more information.
  13. #
  14. # ===========================================================================
  15. #
  16. # Changelog
  17. # ---------
  18. #
  19. # 1.0 Initial version.
  20. #
  21. # ===========================================================================
  22. #
  23. # Description
  24. # -----------
  25. #
  26. # Following CMake variables have to be defined :
  27. #
  28. # VLE_DEBUG - if true, prints debug traces (default OFF)
  29. # VLE_ABI_VERSION - it gives the ABI version of installed vle. For example,
  30. # if VLE_ABI_VERSION = "1.2", then packages are expected to
  31. # be installed into VLE_HOME/pkgs-1.2.
  32. #
  33. # This script provides utils macros for vle cmake files :
  34. #
  35. #####
  36. #
  37. # VleCheckPackage: it checks if a package is installed into the binary
  38. # packages. And sets the variables used to link to the designated package.
  39. #
  40. #> set(VLE_ABI_VERSION 1.2)
  41. #> find_package(VleUtils REQUIRED)
  42. #> VleCheckPackage(MYPKG mypackage)
  43. #> VleCheckPackage(MYPKG2 mypackage2(>=1.0))
  44. #
  45. # The script sets the following variables:
  46. # <MYPKG>_FOUND ... set to 1 if package exists and the version id
  47. # matches the requirement.
  48. # <MYPKG>_LIBRARIES ... only the libraries (w/o the '-l') found into
  49. # the lib directory of the package.
  50. # <MYPKG>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I').
  51. #
  52. #####
  53. #
  54. # VleCheckDependencies: it checks if vle dependencies are installed into the
  55. # binary packages (VLE_HOME/pkgs-${VLE_ABI_VERSION}). Dependencies are
  56. # declared into the flag "Build-Depends" of the file Description.txt
  57. # (${CMAKE_SRC_DIR}/Description.txt).
  58. #
  59. #> set(VLE_ABI_VERSION 1.2)
  60. #> find_package(VleUtils REQUIRED)
  61. #> VleCheckDependencies()
  62. #
  63. #####
  64. #
  65. # VleBuildDynamic: it builds a vle dynamic from a cpp file taking into account
  66. # packages dependencies declared into tag "@@tagdepends" into the cpp file
  67. #
  68. #> set(VLE_ABI_VERSION 1.2)
  69. #> find_package(VleUtils REQUIRED)
  70. #> VleBuildDynamic(Simple "Simple.cpp;otherSouce.cpp")
  71. #
  72. # The Simple.cpp can contain the 'tagdepends', of the form
  73. #
  74. #// @@tagdepends: pkg1,pkg2 @@endtagdepends
  75. #
  76. #####
  77. #
  78. # VleBuildTest: it builds test for the current project, from a cpp file
  79. #
  80. #> set(VLE_ABI_VERSION 1.2)
  81. #> find_package(VleUtils REQUIRED)
  82. #> VleBuildTest(test_pkg "test.cpp")
  83. #
  84. #####
  85. #
  86. # VleBuildOovPlugin: it builds a oov plugin from a cpp file
  87. #
  88. #> set(VLE_ABI_VERSION 1.2)
  89. #> find_package(VleUtils REQUIRED)
  90. #> VleBuildOovPlugin(oovplug_name "source1.cpp;source2.cpp")
  91. #
  92. #####
  93. #
  94. # VleBuildAllDyn: for all the cpp files into the current directory,
  95. # if the tag "@@tagdynamic@@" is present then it builds a vle dynamic
  96. # using VleBuildDynamic.
  97. #
  98. #> set(VLE_ABI_VERSION 1.2)
  99. #> find_package(VleUtils REQUIRED)
  100. #> VleBuildAllDyn()
  101. #
  102. # ===========================================================================
  103. set(VleUtils_VERSION 1.0)
  104. if (NOT DEFINED VLE_DEBUG)
  105. message(FATAL_ERROR "VLE_DEBUG is not set ")
  106. endif ()
  107. if (NOT DEFINED VLE_ABI_VERSION)
  108. message(FATAL_ERROR "VLE_ABI_VERSION is not set ")
  109. endif ()
  110. ##
  111. ## internal macros
  112. ##
  113. macro(_vle_check_package_set var value)
  114. set(${var} "${value}" CACHE INTERNAL "")
  115. endmacro(_vle_check_package_set)
  116. macro(_vle_check_package_unset var)
  117. set(${var} "" CACHE INTERNAL "")
  118. endmacro(_vle_check_package_unset)
  119. ###
  120. # Split module requirement :
  121. # in: ext.muparser(>=1.0)
  122. # out1: ext.muparser
  123. # out2: >=1.0
  124. ###
  125. function(IntVleSplitModRequirement in out1 out2)
  126. if (VLE_DEBUG)
  127. message(STATUS "Enter IntVleSplitModRequirement : '${in}' ")
  128. endif ()
  129. string(REPLACE "(" ";" _module_as_list ${in})
  130. list(LENGTH _module_as_list _module_as_list_length)
  131. if (2 EQUAL _module_as_list_length)
  132. if (VLE_DEBUG)
  133. message(STATUS "Module '${in}' has a version requirement")
  134. endif ()
  135. list(GET _module_as_list 0 _out1tmp)
  136. list(GET _module_as_list 1 _out2tmp)
  137. string(REPLACE ")" "" _out2tmp2 ${_out2tmp})
  138. set (${out1} ${_out1tmp} PARENT_SCOPE)
  139. set (${out2} ${_out2tmp2} PARENT_SCOPE)
  140. else ()
  141. if (1 EQUAL _module_as_list_length)
  142. if (VLE_DEBUG)
  143. message(STATUS "Module '${in}' does not have a version requirement")
  144. endif ()
  145. list(GET _module_as_list 0 _out1tmp)
  146. set (${out1} ${_out1tmp} PARENT_SCOPE)
  147. set (${out2} "" PARENT_SCOPE)
  148. else ()
  149. message(FATAL_ERROR
  150. "Module name format unrecognised : '${in}'")
  151. endif ()
  152. endif()
  153. endfunction()
  154. ###
  155. # Requirement check :
  156. # in1: >=1.0
  157. # in2: 1.0.2
  158. # out: TRUE
  159. ###
  160. function(IntVleRequirementCheck in1 in2 out)
  161. string(LENGTH ${in1} _in1length)
  162. string(SUBSTRING ${in1} 0 2 _in1twoChar)
  163. if (VLE_DEBUG)
  164. message(STATUS "IntVleRequirementCheck two-char comp. ${_in1twoChar}")
  165. message(STATUS "IntVleRequirementCheck in1 ${in1}")
  166. endif ()
  167. string (COMPARE EQUAL ${_in1twoChar} ">=" _greater_eq)
  168. if (${_greater_eq})
  169. math(EXPR _in1ver_length "${_in1length}-2")
  170. string(SUBSTRING ${in1} 2 ${_in1ver_length} _in1ver)
  171. if (VLE_DEBUG)
  172. message(STATUS "IntVleRequirementCheck vers. ${_in1ver}")
  173. endif ()
  174. string(COMPARE EQUAL ${in2} ${_in1ver} _outtmp1)
  175. string(COMPARE GREATER ${in2} ${_in1ver} _outtmp2)
  176. if (_outtmp1 OR _outtmp2)
  177. set (${out} 1 PARENT_SCOPE)
  178. else ()
  179. set (${out} 0 PARENT_SCOPE)
  180. endif ()
  181. return ()
  182. endif ()
  183. string(SUBSTRING ${in1} 0 1 _in1oneChar)
  184. string (COMPARE EQUAL ${_in1oneChar} "=" _equal)
  185. if (${_equal})
  186. math(EXPR _in1ver_length "${_in1length}-1")
  187. string(SUBSTRING ${in1} 1 ${_in1ver_length} _in1ver)
  188. string(COMPARE EQUAL ${in2} ${_in1ver} _outtmp)
  189. if (_outtmp)
  190. set (${out} 1 PARENT_SCOPE)
  191. else ()
  192. set (${out} 0 PARENT_SCOPE)
  193. endif ()
  194. return ()
  195. endif ()
  196. message(FATAL_ERROR "Module version unrecognised : '${in1}'")
  197. endfunction()
  198. ###
  199. # Get the list of build depends of the current package :
  200. # out: mypkg1,mypkg2(>=1.0)
  201. ###
  202. function(IntVleBuildDepends out)
  203. if (NOT EXISTS "${CMAKE_SOURCE_DIR}/Description.txt")
  204. message(FATAL_ERROR "Missing Description.txt file: "
  205. "${CMAKE_SOURCE_DIR}/Description.txt ")
  206. endif ()
  207. file(STRINGS "${CMAKE_SOURCE_DIR}/Description.txt" _description_text)
  208. foreach(_line_descr_text IN LISTS _description_text)
  209. if ("${_line_descr_text}" MATCHES "^Build-Depends:(.*)")
  210. if (NOT ${CMAKE_MATCH_1} STREQUAL "")
  211. string(REGEX REPLACE "[ ]" "" _vle_pkgs_deps ${CMAKE_MATCH_1})
  212. string(REGEX REPLACE "," ";" _vle_pkgs_deps ${_vle_pkgs_deps})
  213. set (${out} "${_vle_pkgs_deps}" PARENT_SCOPE)
  214. return ()
  215. endif ()
  216. endif ()
  217. endforeach ()
  218. endfunction()
  219. ###
  220. # Get the VLE_HOME package directory:
  221. # out: /home/user/.vle/pkgs-1.2
  222. ###
  223. function(IntVleHomePkgs out)
  224. file(TO_CMAKE_PATH "$ENV{VLE_HOME}" _vle_home)
  225. if (NOT _vle_home)
  226. if (CMAKE_HOST_WIN32)
  227. file(TO_CMAKE_PATH "$ENV{HOMEDRIVE}$ENV{HOMEPATH}/vle" _vle_home)
  228. else ()
  229. file(TO_CMAKE_PATH "$ENV{HOME}/.vle" _vle_home)
  230. endif ()
  231. if (_vle_debug)
  232. message(STATUS "The VLE_HOME undefined, try default ${_vle_home}")
  233. endif()
  234. else ()
  235. if (VLE_DEBUG)
  236. message(STATUS "The VLE_HOME defined: ${_vle_home}")
  237. endif ()
  238. endif ()
  239. if (NOT VLE_ABI_VERSION)
  240. set(_vle_package_dir "${_vle_home}/pkgs")
  241. if (VLE_DEBUG)
  242. message(STATUS "VLE_ABI_VERSION undefined for package directory, "
  243. "try default ${_vle_package_dir}")
  244. endif()
  245. else ()
  246. set(_vle_package_dir "${_vle_home}/pkgs-${VLE_ABI_VERSION}")
  247. if (VLE_DEBUG)
  248. message(STATUS "VLE_ABI_VERSION defined for package directory: "
  249. "${_vle_package_dir}")
  250. endif ()
  251. endif ()
  252. if (NOT IS_DIRECTORY ${_vle_package_dir})
  253. message(FATAL_ERROR "Package directory does not exist: ${_vle_package_dir}")
  254. endif ()
  255. set (${out} ${_vle_package_dir} PARENT_SCOPE)
  256. endfunction()
  257. ###
  258. # Get the the version of a installed package from the Description file:
  259. # in: mypkg
  260. # out: 1.0.2
  261. ###
  262. function(IntVleInstalledPkgVersion in out)
  263. IntVleHomePkgs(_vle_pkgs)
  264. if (NOT EXISTS "${_vle_pkgs}/${in}")
  265. message(FATAL_ERROR "Package: '${in}' is missing")
  266. endif ()
  267. set(_descr_file "${_vle_pkgs}/${in}/Description.txt")
  268. if (NOT EXISTS ${_descr_file})
  269. message(FATAL_ERROR "Description file does not exist: ${_descr_file}")
  270. endif ()
  271. file(STRINGS "${_descr_file}" _description_text)
  272. foreach(_line_descr_text IN LISTS _description_text)
  273. if ("${_line_descr_text}" MATCHES "^Version:(.*)")
  274. if (NOT ${CMAKE_MATCH_1} STREQUAL "")
  275. string(REGEX REPLACE "[ ]" "" _pkg_version ${CMAKE_MATCH_1})
  276. set (${out} "${_pkg_version}" PARENT_SCOPE)
  277. else ()
  278. set (${out} "" PARENT_SCOPE)
  279. endif ()
  280. return ()
  281. endif ()
  282. endforeach ()
  283. endfunction()
  284. ######################
  285. ## user visible macros
  286. ######################
  287. macro(VleCheckPackage _prefix _module_with_version)
  288. IntVleSplitModRequirement(${_module_with_version} _module _module_version)
  289. if (NOT ${_module_version} STREQUAL "")
  290. IntVleInstalledPkgVersion(${_module} _installed_mod_version)
  291. IntVleRequirementCheck(
  292. ${_module_version} ${_installed_mod_version} _version_ok)
  293. else ()
  294. set (_version_ok 1)
  295. endif ()
  296. if (NOT _version_ok)
  297. message(FATAL_ERROR "Version of '${module}' not compatible :
  298. required:'${_module_version}', got:'${_installed_mod_version}'")
  299. endif ()
  300. IntVleHomePkgs(_vle_package_dir)
  301. set(_vle_include_dirs "${_vle_package_dir}/${_module}/src")
  302. set(_vle_lib_dirs "${_vle_package_dir}/${_module}/lib")
  303. if (EXISTS "${_vle_include_dirs}" OR EXISTS "${_vle_lib_dirs}")
  304. if (WIN32)
  305. file(GLOB _vle_libraries "${_vle_lib_dirs}/*.a" "${_vle_lib_dirs}/*.dll")
  306. else ()
  307. file(GLOB _vle_libraries "${_vle_lib_dirs}/*.a")
  308. endif ()
  309. _vle_check_package_set(${_prefix}_FOUND 1)
  310. _vle_check_package_set(${_prefix}_INCLUDE_DIRS ${_vle_include_dirs})
  311. _vle_check_package_set(${_prefix}_LIBRARIES "${_vle_libraries}")
  312. if (_vle_debug)
  313. message(STATUS "Found `${_module}': ${_vle_libraries} and "
  314. "${_vle_include_dirs}")
  315. endif ()
  316. else (EXISTS "${_vle_include_dirs}" OR EXISTS "${_vle_lib_dirs}")
  317. message(FATAL_ERROR "Package `${_module}' not found")
  318. endif (EXISTS "${_vle_include_dirs}" OR EXISTS "${_vle_lib_dirs}")
  319. endmacro(VleCheckPackage)
  320. ##
  321. macro(VleCheckDependencies)
  322. IntVleBuildDepends(_vle_pkgs_deps)
  323. foreach(_vle_pkg_dep IN LISTS _vle_pkgs_deps)
  324. IntVleSplitModRequirement(${_vle_pkg_dep} _prefix_pkg _module_version)
  325. VleCheckPackage(${_prefix_pkg} ${_vle_pkg_dep})
  326. if (NOT ${${_prefix_pkg}_FOUND})
  327. message(FATAL_ERROR "Missing package ${_prefix_pkg}")
  328. endif ()
  329. endforeach ()
  330. endmacro(VleCheckDependencies)
  331. ##
  332. macro (VleBuildDynamic _dynname _cppfiles)
  333. if (DEFINED VLE_DEBUG)
  334. set (_vle_debug ${VLE_DEBUG})
  335. else ()
  336. set (_vle_debug 0)
  337. endif ()
  338. set (__cppfilesarg "${_cppfiles}")
  339. list(GET __cppfilesarg 0 __cppfile1)
  340. file(READ "${__cppfile1}" __cppcontent)
  341. string(REGEX MATCH "@@tagdepends:(.+)@@endtagdepends" __tag_depends ${__cppcontent})
  342. set(__include_dirs "")
  343. set(__libraries "")
  344. if(NOT ${__tag_depends} STREQUAL "")
  345. set (__tag_depends ${CMAKE_MATCH_1})
  346. string(REGEX REPLACE "," ";" __tag_depends ${CMAKE_MATCH_1})
  347. foreach(__vle_pkg_dep IN LISTS __tag_depends)
  348. string(REGEX REPLACE "[ ]" "" __vle_pkg_dep ${__vle_pkg_dep})
  349. if (NOT ${__vle_pkg_dep} STREQUAL "")
  350. if (NOT ${__vle_pkg_dep}_FOUND)
  351. message (FATAL_ERROR "Missing pkg '${__vle_pkg_dep}' for building "
  352. "'${__cppfile1}', maybe you forgot to add this "
  353. "package into the Description.txt file")
  354. endif ()
  355. set(__include_dirs "${__include_dirs};${${__vle_pkg_dep}_INCLUDE_DIRS}")
  356. set(__libraries "${__libraries};${${__vle_pkg_dep}_LIBRARIES}")
  357. endif ()
  358. endforeach ()
  359. if (_vle_debug)
  360. message(STATUS "Additional include dir for `${_dynname}': "
  361. "${__include_dirs}")
  362. message(STATUS "Additional libs to link for `${_dynname}': "
  363. "${__libraries}")
  364. endif ()
  365. endif ()
  366. link_directories(${VLE_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS})
  367. add_library(${_dynname} MODULE ${__cppfilesarg})
  368. target_include_directories(${_dynname} PUBLIC
  369. "${CMAKE_SOURCE_DIR}/src;${VLE_INCLUDE_DIRS};"
  370. "${Boost_INCLUDE_DIRS};${__include_dirs}")
  371. target_link_libraries(${_dynname} "${__libraries};${VLE_LIBRARIES};"
  372. "${Boost_LIBRARIES}")
  373. install(TARGETS ${_dynname}
  374. RUNTIME DESTINATION plugins/simulator
  375. LIBRARY DESTINATION plugins/simulator)
  376. endmacro(VleBuildDynamic)
  377. ##
  378. macro (VleBuildTest _testname _cppfile)
  379. link_directories(
  380. ${VLE_LIBRARY_DIRS}
  381. ${Boost_LIBRARY_DIRS})
  382. add_executable(${_testname} ${_cppfile})
  383. target_include_directories(${_testname} PUBLIC
  384. "${CMAKE_SOURCE_DIR}/src;${VLE_INCLUDE_DIRS};${Boost_INCLUDE_DIRS}")
  385. target_link_libraries(${_testname}
  386. ${VLE_LIBRARIES}
  387. ${Boost_LIBRARIES}
  388. ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
  389. ${Boost_DATE_TIME_LIBRARY})
  390. add_test(${_testname} ${_testname})
  391. endmacro (VleBuildTest)
  392. ##
  393. macro (VleBuildOovPlugin _pluginname _cppfile)
  394. link_directories(${VLE_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS})
  395. add_library(${_pluginname} SHARED ${_cppfile})
  396. target_include_directories(${_pluginname} PUBLIC
  397. "${CMAKE_SOURCE_DIR}/src;${VLE_INCLUDE_DIRS};${Boost_INCLUDE_DIRS};"
  398. "${CMAKE_BINARY_DIR}/src")
  399. target_link_libraries(${_pluginname} ${VLE_LIBRARIES})
  400. INSTALL(TARGETS ${_pluginname}
  401. RUNTIME DESTINATION plugins/output
  402. LIBRARY DESTINATION plugins/output)
  403. endmacro (VleBuildOovPlugin)
  404. ##
  405. macro (VleBuildAllDyn)
  406. if (DEFINED VLE_DEBUG)
  407. set (_vle_debug ${VLE_DEBUG})
  408. else ()
  409. set (_vle_debug 0)
  410. endif ()
  411. file(GLOB __cpp_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp")
  412. foreach(__cppfile ${__cpp_files})
  413. file(READ "${__cppfile}" __cppcontent)
  414. string(REGEX MATCH "@@tagdynamic@@+" __tag_dyn ${__cppcontent})
  415. if(NOT ${__tag_dyn} STREQUAL "")
  416. string(REGEX MATCH "(.+).cpp" __libname ${__cppfile})
  417. set (__libname ${CMAKE_MATCH_1})
  418. if (_vle_debug)
  419. message(STATUS "Detected lib to build : `${__libname}'")
  420. endif ()
  421. VleBuildDynamic(${__libname} "${__cppfile}")
  422. endif()
  423. endforeach ()
  424. endmacro (VleBuildAllDyn)