## ========================================================================= ## @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co) ## ========================================================================= ## ------------------------------------------------------------------------- function(BuildLibrary lib typ) set(options RECURRENT INSTALL_ALL INSTALL_BIN INSTALL_DEV) set(oneValueArgs) set(multiValueArgs SOURCE VERSION LINKS) cmake_parse_arguments( BuildLibrary "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) ## -- Get sources set(_files) foreach(_s ${BuildLibrary_SOURCE}) ## -- Canonicalize path get_filename_component(_p "${_s}" ABSOLUTE) ## -- Check type of input if(IS_DIRECTORY ${_p}) if(BuildLibrary_RECURRENT) file(GLOB_RECURSE _f "${_p}/*") else(BuildLibrary_RECURRENT) file(GLOB _f "${_p}/*") endif(BuildLibrary_RECURRENT) foreach(_x ${_f}) if(NOT IS_DIRECTORY ${_x}) list(APPEND _files ${_x}) endif(NOT IS_DIRECTORY ${_x}) endforeach(_x) else(IS_DIRECTORY ${_p}) list(APPEND _files ${_p}) endif(IS_DIRECTORY ${_p}) endforeach(_s) ## -- Process sources set(_cpp) set(_hpp) set(_qui) foreach(_f ${_files}) ## -- Separate filename from extension string(REGEX REPLACE "\\.[^.]*$" "" _name ${_f}) string(REPLACE ${_name} "" _ext ${_f}) set(_out_name ${_name}) set(_out_ext ${_ext}) if(_out_ext) ## -- Process .in files string(COMPARE EQUAL "${_ext}" ".in" _in_cmp) if(_in_cmp) string(REPLACE ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} _out ${_name}) configure_file(${_f} ${_out} @ONLY) string(REGEX REPLACE "\\.[^.]*$" "" _out_name ${_out}) string(REPLACE ${_out_name} "" _out_ext ${_out}) endif(_in_cmp) ## -- Now, get real extension string(SUBSTRING ${_out_ext} 0 2 _ext_cmp) ## -- Process .c?? files string(COMPARE EQUAL "${_ext_cmp}" ".c" _c_cmp) if(_c_cmp) list(APPEND _cpp ${_out_name}${_out_ext}) endif(_c_cmp) ## -- Process .h?? files string(COMPARE EQUAL "${_ext_cmp}" ".h" _h_cmp) if(_h_cmp) list(APPEND _hpp ${_out_name}${_out_ext}) endif(_h_cmp) ## -- Process .ui files string(COMPARE EQUAL "${_out_ext}" ".ui" _u_cmp) if(_u_cmp) list(APPEND _qui ${_out_name}${_out_ext}) endif(_u_cmp) endif(_out_ext) endforeach(_f) ## -- Process Qt ui files list(LENGTH _qui _qui_len) if(${_qui_len} GREATER 0) qt5_wrap_ui(_qui_hpp ${_qui}) endif(${_qui_len} GREATER 0) if(_cpp) ## -- Real build add_library(${lib} ${typ} ${_cpp} ${_hpp} ${_qui_hpp}) ## -- Header creation generate_export_header(${lib}) ## -- Put version strings if(BuildLibrary_VERSION) list(GET BuildLibrary_VERSION 0 _a) list(GET BuildLibrary_VERSION 1 _b) list(GET BuildLibrary_VERSION 2 _c) set_property(TARGET ${lib} PROPERTY VERSION "${_a}.${_b}.${_c}") set_property(TARGET ${lib} PROPERTY SOVERSION ${_a}) set_property(TARGET ${lib} PROPERTY INTERFACE_${lib}_MAJOR_VERSION ${_a}) set_property( TARGET ${lib} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${_a} ) endif(BuildLibrary_VERSION) ## -- Link library if(BuildLibrary_LINKS) target_link_libraries(${lib} PUBLIC ${BuildLibrary_LINKS}) endif(BuildLibrary_LINKS) endif(_cpp) ## -- Installation rules if(BuildLibrary_INSTALL_ALL) set(BuildLibrary_INSTALL_BIN ${BuildLibrary_INSTALL_ALL}) set(BuildLibrary_INSTALL_DEV ${BuildLibrary_INSTALL_ALL}) endif(BuildLibrary_INSTALL_ALL) ## -- Installation rules if(_cpp) if(BuildLibrary_INSTALL_BIN) install( TARGETS ${lib} EXPORT "${targets_export_name}" LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib" RUNTIME DESTINATION "bin" INCLUDES DESTINATION "${include_install_dir}" ) endif(BuildLibrary_INSTALL_BIN) endif(_cpp) if(BuildLibrary_INSTALL_DEV) string(TOLOWER ${lib} _lower_lib) set(_install_hdr ${_hpp}) if(_cpp) list( APPEND _install_hdr ${CMAKE_CURRENT_BINARY_DIR}/${_lower_lib}_export.h ) endif(_cpp) set(_install_dirs) foreach(_h ${_install_hdr}) string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "" _h_name ${_h}) string(COMPARE EQUAL "${_h_name}" "${_h}" _h_cmp) if(_h_cmp) string(REPLACE ${CMAKE_CURRENT_BINARY_DIR} "" _h_name ${_h}) endif(_h_cmp) set(_h_out ${include_install_dir}/${lib}${_h_name}) get_filename_component(_h_dir ${_h_out} DIRECTORY) install( FILES "${_h}" DESTINATION "${_h_dir}" ) endforeach(_h) endif(BuildLibrary_INSTALL_DEV) endfunction() ## ------------------------------------------------------------------------- function(BuildApplication app) set(options INSTALL RECURRENT) set(oneValueArgs) set(multiValueArgs SOURCE LINKS) cmake_parse_arguments( BuildApplication "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) set(_lib __lib__${app}) if(BuildApplication_RECURRENT) BuildLibrary( ${_lib} STATIC RECURRENT SOURCE ${BuildApplication_SOURCE} LINKS ${BuildApplication_LINKS} ) else(BuildApplication_RECURRENT) BuildLibrary( ${_lib} STATIC SOURCE ${BuildApplication_SOURCE} LINKS ${BuildApplication_LINKS} ) endif(BuildApplication_RECURRENT) ## -- Create an empty application set(_m ${CMAKE_CURRENT_BINARY_DIR}/__main__${app}.cxx) file(WRITE ${_m} "// Automatically generated dummy file") add_executable(${app} ${EXECUTABLE_TYPE} ${_m}) ## -- Link it against the static library target_link_libraries(${app} PUBLIC ${_lib}) ## -- Installation rules if(BuildApplication_INSTALL) install( TARGETS ${app} EXPORT "${targets_export_name}" LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib" RUNTIME DESTINATION "bin" INCLUDES DESTINATION "${include_install_dir}" ) endif(BuildApplication_INSTALL) endfunction() ## eof - $RCSfile$