X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=cmake%2FFunctions.cmake;h=d7555872cda546b799c5ee0a9e418b3bb48252a4;hb=61b3659afe961ed248f30e26f9ca8f28fcfafddc;hp=02ec213e50c7334162861a890d6f6c3efd20c357;hpb=47b6ec1790c1c9cb9c8396168e73f1c9f3c764bc;p=cpPlugins.git diff --git a/cmake/Functions.cmake b/cmake/Functions.cmake index 02ec213..d755587 100644 --- a/cmake/Functions.cmake +++ b/cmake/Functions.cmake @@ -60,6 +60,7 @@ ENDFUNCTION() ## ------------------------------------------------------------------------- FUNCTION( PrepareSourceFiles + lib_name out_sources_list out_headers_list out_headers_paths @@ -69,6 +70,7 @@ SET(_sources_extensions .c .cpp .cxx) SET(_headers_extensions .h .hpp .hxx) SET(_qt_ui_extensions .ui) SET(_demangler_extensions .d) +SET(_instances_extensions .i) ## -- Configure inputs SET(_all_files) @@ -98,12 +100,14 @@ SET(_srcs) SET(_hdrs) SET(_qts) SET(_demanglers) +SET(_instances) FOREACH(_file ${_all_files}) GET_FILENAME_COMPONENT(_ext ${_file} EXT) LIST(FIND _sources_extensions ${_ext} _src) LIST(FIND _headers_extensions ${_ext} _hdr) LIST(FIND _qt_ui_extensions ${_ext} _ui) LIST(FIND _demangler_extensions ${_ext} _dem) + LIST(FIND _instances_extensions ${_ext} _ins) IF(NOT ${_src} EQUAL -1) LIST(APPEND _srcs ${_file}) ENDIF(NOT ${_src} EQUAL -1) @@ -116,6 +120,9 @@ FOREACH(_file ${_all_files}) IF(NOT ${_dem} EQUAL -1) LIST(APPEND _demanglers ${_file}) ENDIF(NOT ${_dem} EQUAL -1) + IF(NOT ${_ins} EQUAL -1) + LIST(APPEND _instances ${_file}) + ENDIF(NOT ${_ins} EQUAL -1) ENDFOREACH(_file) # -- Prepare Qt4-based code @@ -156,6 +163,35 @@ FOREACH(_d ${_demanglers}) LIST(APPEND _hdrs ${_d_out}) ENDFOREACH(_d) +## -- Create instances +FOREACH(_i ${_instances}) + STRING( + REPLACE + ${PROJECT_SOURCE_DIR} + ${PROJECT_BINARY_DIR} + _i_bin + ${_i} + ) + GET_FILENAME_COMPONENT(_i_path ${_i_bin} DIRECTORY) + GET_FILENAME_COMPONENT(_out_name ${_i} NAME_WE) + + ## -- Infere source code filenames + MATH(EXPR _last_range "${cpPlugins_NUMBER_OF_FILES}-1") + SET(_out_code) + FOREACH(_n RANGE 0 ${_last_range}) + LIST(APPEND _out_code ${_i_path}/${_out_name}_${_n}.cxx) + LIST(APPEND _srcs ${_i_path}/${_out_name}_${_n}.cxx) + ENDFOREACH(_n) + + ## -- Command to write source code + ADD_CUSTOM_COMMAND( + OUTPUT ${_out_code} + DEPENDS ${cpPlugins_bash_CreateInstances_APP} ${_i} + COMMAND ${CMAKE_COMMAND} -E make_directory ${_i_path} + COMMAND ${cpPlugins_bash_CreateInstances_APP} ${_i} ${lib_name} ${_i_path}/${_out_name} + ) +ENDFOREACH(_i) + ## -- Real compilation SET(_hdrs_paths) FOREACH(_hdr ${_hdrs}) @@ -173,18 +209,29 @@ SET(${out_headers_paths} ${_hdrs_paths} PARENT_SCOPE) ENDFUNCTION() ## ------------------------------------------------------------------------- -FUNCTION( - CreateLib - lib_name - lib_type - ) -PrepareSourceFiles(_srcs _hdrs _paths ${ARGN}) -INCLUDE_DIRECTORIES( - ${_paths} - ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_BINARY_DIR} - ) +FUNCTION(cpPlugins_BuildLibrary lib_name lib_type) +# -- Detect all source files +SET(_all_files) +FOREACH(_c ${ARGN}) + GET_FILENAME_COMPONENT(_cname ${_c} ABSOLUTE) + SET(_files) + IF(IS_DIRECTORY ${_cname}) + FILE(GLOB_RECURSE _files "${_cname}/*") + ELSE(IS_DIRECTORY ${_cname}) + SET(_files ${_cname}) + ENDIF(IS_DIRECTORY ${_cname}) + LIST(APPEND _all_files ${_files}) +ENDFOREACH(_c ${ARGN}) + +## -- Prepare sources by types +PrepareSourceFiles(${lib_name} _srcs _hdrs _paths ${_all_files}) + +## -- Build library IF(_srcs) + INCLUDE_DIRECTORIES( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ) ADD_LIBRARY(${lib_name} ${lib_type} ${_srcs} ${_hdrs}) GENERATE_EXPORT_HEADER( ${lib_name} @@ -193,152 +240,89 @@ IF(_srcs) EXPORT_FILE_NAME ${lib_name}_Export.h STATIC_DEFINE ${lib_name}_BUILT_AS_STATIC ) - IF(${prj_NAME}_All_Headers) - SET(_hdr_tmp "${${prj_NAME}_All_Headers};${_hdrs}") - ELSE(${prj_NAME}_All_Headers) - SET(_hdr_tmp "${_hdrs}") - ENDIF(${prj_NAME}_All_Headers) - LIST(REMOVE_DUPLICATES _hdr_tmp) - SET(${prj_NAME}_All_Headers ${_hdr_tmp} CACHE INTERNAL "All headers." FORCE) ENDIF(_srcs) -ENDFUNCTION() - -## ------------------------------------------------------------------------- -FUNCTION( - CompileLibFromDir - lib_name - lib_type - lib_dir - ) -FILE(GLOB_RECURSE _files "${lib_dir}/*") -CreateLib(${lib_name} ${lib_type} ${_files} ${ARGN}) -ENDFUNCTION() - -## ------------------------------------------------------------------------- -FUNCTION( - CompileInstances - out_lib_name - def_file - number_of_files - ) - -### -- Configure inputs to be cmake-path compatible -SET(_src_dir ${CMAKE_CURRENT_SOURCE_DIR}) -SET(_bin_dir ${CMAKE_CURRENT_BINARY_DIR}) -SET(_def_file "${_src_dir}/${def_file}") - -## -- Infere source code filenames -MATH(EXPR _last_range "${number_of_files}-1") -SET(_out_code "${_bin_dir}/${out_lib_name}.h") -FOREACH(_n RANGE 0 ${_last_range}) - LIST(APPEND _out_code ${_bin_dir}/${out_lib_name}_${_n}.cxx) -ENDFOREACH(_n) - -## -- Command to write source code -ADD_CUSTOM_COMMAND( - OUTPUT ${_out_code} - DEPENDS ${cpPlugins_bash_CreateInstances_APP} ${_def_file} - COMMAND ${cpPlugins_bash_CreateInstances_APP} ${_def_file} ${out_lib_name} ${_bin_dir} ${number_of_files} - ) - -## -- Create library -CreateLib(${out_lib_name} SHARED "${_out_code}") ENDFUNCTION() ## ------------------------------------------------------------------------- -FUNCTION( - CreatePlugin - lib_name - lib_dir - number_of_files - ) - -GET_FILENAME_COMPONENT(_lib_dir ${lib_dir} REALPATH) -FILE(GLOB_RECURSE _files "${_lib_dir}/*") -SET(_hdr_to_wrap) -SET(_instances) -FOREACH(_file ${_files}) - FILE(READ ${_file} _txt) - STRING(FIND "${_txt}" "cpPluginsObject" _res) - IF(NOT ${_res} EQUAL -1) - LIST(APPEND _hdr_to_wrap ${_file}) - ENDIF(NOT ${_res} EQUAL -1) - GET_FILENAME_COMPONENT(_ext ${_file} EXT) - IF(${_ext} STREQUAL ".i") - LIST(APPEND _instances ${_file}) - ENDIF(${_ext} STREQUAL ".i") -ENDFOREACH(_file) +FUNCTION(cpPlugins_BuildPluginsLibrary lib_name) +# -- Detect all source files +SET(_all_files) +FOREACH(_c ${ARGN}) + GET_FILENAME_COMPONENT(_cname ${_c} ABSOLUTE) + SET(_files) + IF(IS_DIRECTORY ${_cname}) + FILE(GLOB_RECURSE _files "${_cname}/*") + ELSE(IS_DIRECTORY ${_cname}) + SET(_files ${_cname}) + ENDIF(IS_DIRECTORY ${_cname}) + LIST(APPEND _all_files ${_files}) +ENDFOREACH(_c ${ARGN}) -IF(_instances) - FOREACH(_instance ${_instances}) - STRING( - REPLACE - ${PROJECT_SOURCE_DIR} - ${PROJECT_BINARY_DIR} - _out_dir - ${_lib_dir} - ) - MATH(EXPR _last_range "${number_of_files}-1") - SET(_out_code "${_out_dir}/${lib_name}.h") - FOREACH(_n RANGE 0 ${_last_range}) - LIST(APPEND _out_code ${_out_dir}/${lib_name}_${_n}.cxx) - ENDFOREACH(_n) +## -- Prepare sources by types +PrepareSourceFiles(${lib_name} _srcs _hdrs _paths ${_all_files}) - ADD_CUSTOM_COMMAND( - OUTPUT ${_out_code} - DEPENDS ${cpPlugins_bash_CreateInstances_APP} ${_instance} - COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir} - COMMAND ${cpPlugins_bash_CreateInstances_APP} ${_instance} ${lib_name} ${_out_dir} ${number_of_files} - ) - LIST(APPEND _files ${_out_code}) - ENDFOREACH(_instance) -ENDIF(_instances) +## -- Check which headers need to be wrapped to build host code +SET(_hdrs_to_wrap) +FOREACH(_hdr ${_hdrs}) + IF(EXISTS ${_hdr}) + FILE(READ ${_hdr} _txt) + STRING(FIND "${_txt}" "cpPluginsObject" _res) + IF(NOT ${_res} EQUAL -1) + LIST(APPEND _hdrs_to_wrap ${_hdr}) + ENDIF(NOT ${_res} EQUAL -1) + ENDIF(EXISTS ${_hdr}) +ENDFOREACH(_hdr) -IF(_hdr_to_wrap) - STRING( - REPLACE - ${PROJECT_SOURCE_DIR} - ${PROJECT_BINARY_DIR} - _out_dir - ${_lib_dir} - ) - SET(_host ${_out_dir}/${lib_name}_host.cxx) +## -- Wrap headers +IF(_hdrs_to_wrap) + SET(_host ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}_host.cxx) ADD_CUSTOM_COMMAND( OUTPUT ${_host} - DEPENDS ${cpPlugins_bash_HostCreator_APP} ${_hdr_to_wrap} - COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir} - COMMAND ${cpPlugins_bash_HostCreator_APP} ${lib_name} ${_host} ${_hdr_to_wrap} + DEPENDS ${cpPlugins_bash_HostCreator_APP} ${_hdrs_to_wrap} + COMMAND ${cpPlugins_bash_HostCreator_APP} ${lib_name} ${_host} ${_hdrs_to_wrap} ) - LIST(APPEND _files ${_host}) -ENDIF(_hdr_to_wrap) - -CreateLib(${lib_name} SHARED ${_files}) + LIST(APPEND _all_files ${_host}) +ENDIF(_hdrs_to_wrap) +cpPlugins_BuildLibrary(${lib_name} SHARED ${_all_files}) ENDFUNCTION() ## ------------------------------------------------------------------------- -FUNCTION( - CompileAppFromDir - app_name - app_dir - ) -FILE(GLOB_RECURSE _files "${app_dir}/*") -PrepareSourceFiles(_srcs _hdrs _paths ${_files} ${ARGN}) -INCLUDE_DIRECTORIES( - ${_paths} - ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_BINARY_DIR} - ) -IF(_srcs) - SET(_app_os_target) - IF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - SET(_app_os_target WIN32) - ELSEIF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") - SET(_app_os_target MACOSX_BUNDLE) - ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - ADD_EXECUTABLE(${app_name} ${_app_os_target} ${_srcs} ${_hdrs}) -ENDIF(_srcs) +FUNCTION(cpPlugins_BuildApplication app_name) +OPTION(BUILD_${app_name} "Build \"${app_name}\" application?" OFF) +IF(BUILD_${app_name}) + # -- Detect all source files + SET(_all_files) + FOREACH(_c ${ARGN}) + GET_FILENAME_COMPONENT(_cname ${_c} ABSOLUTE) + SET(_files) + IF(IS_DIRECTORY ${_cname}) + FILE(GLOB_RECURSE _files "${_cname}/*") + ELSE(IS_DIRECTORY ${_cname}) + SET(_files ${_cname}) + ENDIF(IS_DIRECTORY ${_cname}) + LIST(APPEND _all_files ${_files}) + ENDFOREACH(_c ${ARGN}) + + ## -- Prepare sources by types + PrepareSourceFiles(${app_name} _srcs _hdrs _paths ${_all_files}) + + ## -- Build library + IF(_srcs) + INCLUDE_DIRECTORIES( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ) + SET(_app_os_target) + IF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") + SET(_app_os_target WIN32) + ELSEIF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") + SET(_app_os_target MACOSX_BUNDLE) + ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") + ADD_EXECUTABLE(${app_name} ${_app_os_target} ${_srcs} ${_hdrs}) + ENDIF(_srcs) +ENDIF(BUILD_${app_name}) ENDFUNCTION() ## eof - $RCSfile$