From 3aea74d40887a0280416bdff76152b5bcced9a00 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Sun, 24 Jul 2011 19:22:58 +0000 Subject: [PATCH] Fixed precompiled headers support (fixed formatting, fixed dependencies generation) --- OpenCVModule.cmake | 69 +++--- OpenCVPCHSupport.cmake | 392 ++++++++++++++++----------------- modules/gpu/CMakeLists.txt | 67 ++---- modules/highgui/CMakeLists.txt | 34 +-- 4 files changed, 249 insertions(+), 313 deletions(-) diff --git a/OpenCVModule.cmake b/OpenCVModule.cmake index d69a350ab7..389a10b7e7 100644 --- a/OpenCVModule.cmake +++ b/OpenCVModule.cmake @@ -1,3 +1,24 @@ +#opencv precompiled headers macro (can add pch to modules and tests) +#this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work +macro(add_opencv_precompiled_headers the_target) + if(the_target MATCHES opencv_test_) + SET(pch_name "test/test_precomp") + else() + SET(pch_name "src/precomp") + endif() + set(pch_header "${CMAKE_CURRENT_SOURCE_DIR}/${pch_name}.hpp") + if(PCHSupport_FOUND AND USE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}") + if(CMAKE_GENERATOR MATCHES Visual) + set(${the_target}_pch "${CMAKE_CURRENT_SOURCE_DIR}/${pch_name}.cpp") + add_native_precompiled_header(${the_target} ${pch_header}) + elseif(CMAKE_GENERATOR MATCHES Xcode) + add_native_precompiled_header(${the_target} ${pch_header}) + elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_GENERATOR MATCHES Makefiles) + add_precompiled_header(${the_target} ${pch_header}) + endif() + endif() +endmacro() + # this is template for a OpenCV module macro(define_opencv_module name) @@ -8,20 +29,19 @@ macro(define_opencv_module name) "${CMAKE_CURRENT_BINARY_DIR}") foreach(d ${ARGN}) - if(${d} MATCHES "opencv_") - if(${d} MATCHES "opencv_lapack") - else() - string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) - include_directories("${d_dir}/include") - endif() + if(d MATCHES "opencv_") + string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) + include_directories("${d_dir}/include") endif() endforeach() file(GLOB lib_srcs "src/*.cpp") file(GLOB lib_int_hdrs "src/*.h*") + if(COMMAND get_module_external_sources) get_module_external_sources(${name}) endif() + source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs}) file(GLOB lib_hdrs "include/opencv2/${name}/*.h*") @@ -66,17 +86,7 @@ macro(define_opencv_module name) INSTALL_NAME_DIR lib ) - if(PCHSupport_FOUND AND USE_PRECOMPILED_HEADERS) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_target}_pch "src/precomp.cpp") - endif() - add_native_precompiled_header(${the_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_target} ${pch_header}) - endif() - endif() + add_opencv_precompiled_headers(${the_target}) # Add the required libraries for linking: target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN}) @@ -111,12 +121,9 @@ macro(define_opencv_module name) set(test_deps opencv_${name} ${ARGN} opencv_ts opencv_highgui ${EXTRA_${the_target}_DEPS}) foreach(d ${test_deps}) - if(${d} MATCHES "opencv_") - if(${d} MATCHES "opencv_lapack") - else() - string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) - include_directories("${d_dir}/include") - endif() + if(d MATCHES "opencv_") + string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) + include_directories("${d_dir}/include") endif() endforeach() @@ -130,24 +137,14 @@ macro(define_opencv_module name) add_executable(${the_target} ${test_srcs} ${test_hdrs}) - if(PCHSupport_FOUND AND USE_PRECOMPILED_HEADERS) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/test/test_precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_target}_pch "test/test_precomp.cpp") - endif() - add_native_precompiled_header(${the_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_target} ${pch_header}) - endif() - endif() + add_opencv_precompiled_headers(${the_target}) # Additional target properties set_target_properties(${the_target} PROPERTIES DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}" ) - + if(ENABLE_SOLUTION_FOLDERS) set_target_properties(${the_target} PROPERTIES FOLDER "tests") endif() @@ -165,5 +162,5 @@ macro(define_opencv_module name) # install(TARGETS ${the_target} RUNTIME DESTINATION bin COMPONENT main) #endif() endif() - + endmacro() diff --git a/OpenCVPCHSupport.cmake b/OpenCVPCHSupport.cmake index dae36d1e68..d0acecceb1 100644 --- a/OpenCVPCHSupport.cmake +++ b/OpenCVPCHSupport.cmake @@ -15,234 +15,222 @@ IF(CMAKE_COMPILER_IS_GNUCXX) EXEC_PROGRAM( - ${CMAKE_CXX_COMPILER} - ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion + ${CMAKE_CXX_COMPILER} + ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion OUTPUT_VARIABLE gcc_compiler_version) #MESSAGE("GCC Version: ${gcc_compiler_version}") IF(gcc_compiler_version MATCHES "4\\.[0,2-9]\\.[0-9]") SET(PCHSupport_FOUND TRUE) ENDIF() - SET(_PCH_include_prefix "-I") + SET(_PCH_include_prefix "-I") +ELSEIF(WIN32) + SET(PCHSupport_FOUND TRUE) # for experimental msvc support + SET(_PCH_include_prefix "/I") ELSE() - IF(WIN32) - SET(PCHSupport_FOUND TRUE) # for experimental msvc support - SET(_PCH_include_prefix "/I") - ELSE() - SET(PCHSupport_FOUND FALSE) - ENDIF() -ENDIF(CMAKE_COMPILER_IS_GNUCXX) + SET(PCHSupport_FOUND FALSE) +ENDIF() MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags) + STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name) + SET(${_out_compile_flags} ${${_flags_var_name}} ) - STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name) - SET(${_out_compile_flags} ${${_flags_var_name}} ) + IF(CMAKE_COMPILER_IS_GNUCXX) - IF(CMAKE_COMPILER_IS_GNUCXX) + GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE) + IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32) + LIST(APPEND ${_out_compile_flags} "-fPIC") + ENDIF() - GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE) - IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32) - LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC") + ELSE() + ## TODO ... ? or does it work out of the box ENDIF() - ELSE() - ## TODO ... ? or does it work out of the box - ENDIF() + GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES ) + FOREACH(item ${DIRINC}) + LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"") + ENDFOREACH(item) - GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES ) - FOREACH(item ${DIRINC}) - LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"") - ENDFOREACH(item) + GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS) + GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${OpenCV_SOURCE_DIR} DEFINITIONS) + #MESSAGE("_directory_flags ${_directory_flags} ${_global_definitions}" ) + LIST(APPEND ${_out_compile_flags} ${_directory_flags}) + LIST(APPEND ${_out_compile_flags} ${_global_definitions}) + LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS}) - GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS) - GET_DIRECTORY_PROPERTY(_global_definitions DIRECTORY ${OpenCV_SOURCE_DIR} DEFINITIONS) - #MESSAGE("_directory_flags ${_directory_flags} ${_global_definitions}" ) - LIST(APPEND ${_out_compile_flags} ${_directory_flags}) - LIST(APPEND ${_out_compile_flags} ${_global_definitions}) - LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS} ) - - SEPARATE_ARGUMENTS(${_out_compile_flags}) + SEPARATE_ARGUMENTS(${_out_compile_flags}) ENDMACRO(_PCH_GET_COMPILE_FLAGS) MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp) - SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx) - FILE(WRITE ${${_dephelp}} -"#include \"${_include_file}\" -int testfunction() -{ - return 0; -} -" - ) + SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx) + ADD_CUSTOM_COMMAND( + OUTPUT "${${_dephelp}}" + COMMAND ${CMAKE_COMMAND} -E echo "#include \\\"${_include_file}\\\"" > "${${_dephelp}}" + COMMAND ${CMAKE_COMMAND} -E echo "int testfunction()" >> "${${_dephelp}}" + COMMAND ${CMAKE_COMMAND} -E echo "{" >> "${${_dephelp}}" + COMMAND ${CMAKE_COMMAND} -E echo " return 0;" >> "${${_dephelp}}" + COMMAND ${CMAKE_COMMAND} -E echo "}" >> "${${_dephelp}}" + DEPENDS "${_include_file}" + ) ENDMACRO(_PCH_WRITE_PCHDEP_CXX ) MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output) - FILE(TO_NATIVE_PATH ${_input} _native_input) - FILE(TO_NATIVE_PATH ${_output} _native_output) + FILE(TO_NATIVE_PATH ${_input} _native_input) + FILE(TO_NATIVE_PATH ${_output} _native_output) - - IF(CMAKE_COMPILER_IS_GNUCXX) - IF(CMAKE_CXX_COMPILER_ARG1) - # remove leading space in compiler argument + IF(CMAKE_COMPILER_IS_GNUCXX) + IF(CMAKE_CXX_COMPILER_ARG1) + # remove leading space in compiler argument STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1}) - SET(${out_command} - ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input} - ) - ELSE(CMAKE_CXX_COMPILER_ARG1) - SET(${out_command} - ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input} - ) - ENDIF(CMAKE_CXX_COMPILER_ARG1) - ELSE(CMAKE_COMPILER_IS_GNUCXX) + SET(${out_command} + ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input} + ) + ELSE(CMAKE_CXX_COMPILER_ARG1) + SET(${out_command} + ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input} + ) + ENDIF(CMAKE_CXX_COMPILER_ARG1) + ELSE(CMAKE_COMPILER_IS_GNUCXX) - SET(_dummy_str "#include <${_input}>") - FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str}) + SET(_dummy_str "#include <${_input}>") + FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str}) - SET(${out_command} - ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp - ) - #/out:${_output} + SET(${out_command} + ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp + ) + #/out:${_output} - ENDIF(CMAKE_COMPILER_IS_GNUCXX) + ENDIF(CMAKE_COMPILER_IS_GNUCXX) ENDMACRO(_PCH_GET_COMPILE_COMMAND ) - MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn ) - FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path) + FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path) - IF(CMAKE_COMPILER_IS_GNUCXX) - # for use with distcc and gcc >4.0.1 if preprocessed files are accessible - # on all remote machines set - # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess - # if you want warnings for invalid header files (which is very inconvenient - # if you have different versions of the headers for different build types - # you may set _pch_dowarn - IF (_dowarn) - SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" -Winvalid-pch " ) - ELSE (_dowarn) - SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" " ) - ENDIF (_dowarn) - ELSE(CMAKE_COMPILER_IS_GNUCXX) + IF(CMAKE_COMPILER_IS_GNUCXX) + # for use with distcc and gcc >4.0.1 if preprocessed files are accessible + # on all remote machines set + # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess + # if you want warnings for invalid header files (which is very inconvenient + # if you have different versions of the headers for different build types + # you may set _pch_dowarn + IF (_dowarn) + SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" -Winvalid-pch " ) + ELSE (_dowarn) + SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" " ) + ENDIF (_dowarn) - set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" ) + ELSE(CMAKE_COMPILER_IS_GNUCXX) - ENDIF(CMAKE_COMPILER_IS_GNUCXX) + set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" ) + + ENDIF(CMAKE_COMPILER_IS_GNUCXX) ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS ) + MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output) - GET_FILENAME_COMPONENT(_name ${_input} NAME) - GET_FILENAME_COMPONENT(_path ${_input} PATH) - SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch") + + GET_FILENAME_COMPONENT(_name ${_input} NAME) + GET_FILENAME_COMPONENT(_path ${_input} PATH) + SET(${_output} "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch") + ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input) MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use ) - # to do: test whether compiler flags match between target _targetName - # and _pch_output_to_use - GET_FILENAME_COMPONENT(_name ${_input} NAME) + # to do: test whether compiler flags match between target _targetName + # and _pch_output_to_use + GET_FILENAME_COMPONENT(_name ${_input} NAME) - IF( "${ARGN}" STREQUAL "0") - SET(_dowarn 0) - ELSE( "${ARGN}" STREQUAL "0") - SET(_dowarn 1) - ENDIF("${ARGN}" STREQUAL "0") + IF(ARGN STREQUAL "0") + SET(_dowarn 0) + ELSE() + SET(_dowarn 1) + ENDIF() + _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn}) + #MESSAGE("Add flags ${_target_cflags} to ${_targetName} " ) + SET_TARGET_PROPERTIES(${_targetName} + PROPERTIES + COMPILE_FLAGS ${_target_cflags} + ) - _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn}) - # MESSAGE("Add flags ${_target_cflags} to ${_targetName} " ) - SET_TARGET_PROPERTIES(${_targetName} - PROPERTIES - COMPILE_FLAGS ${_target_cflags} - ) + ADD_CUSTOM_TARGET(pch_Generate_${_targetName} + DEPENDS ${_pch_output_to_use} + ) - ADD_CUSTOM_TARGET(pch_Generate_${_targetName} - DEPENDS ${_pch_output_to_use} - ) - - ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} ) + ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} ) ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET) MACRO(ADD_PRECOMPILED_HEADER _targetName _input) - SET(_PCH_current_target ${_targetName}) + SET(_PCH_current_target ${_targetName}) - IF(NOT CMAKE_BUILD_TYPE) - MESSAGE(FATAL_ERROR - "This is the ADD_PRECOMPILED_HEADER macro. " - "You must set CMAKE_BUILD_TYPE!" - ) - ENDIF(NOT CMAKE_BUILD_TYPE) + IF(NOT CMAKE_BUILD_TYPE) + MESSAGE(FATAL_ERROR + "This is the ADD_PRECOMPILED_HEADER macro. " + "You must set CMAKE_BUILD_TYPE!" + ) + ENDIF() - IF( "${ARGN}" STREQUAL "0") - SET(_dowarn 0) - ELSE( "${ARGN}" STREQUAL "0") - SET(_dowarn 1) - ENDIF("${ARGN}" STREQUAL "0") + IF(ARGN STREQUAL "0") + SET(_dowarn 0) + ELSE() + SET(_dowarn 1) + ENDIF() + GET_FILENAME_COMPONENT(_name ${_input} NAME) + GET_FILENAME_COMPONENT(_path ${_input} PATH) + GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output) - GET_FILENAME_COMPONENT(_name ${_input} NAME) - GET_FILENAME_COMPONENT(_path ${_input} PATH) - GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output) + _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx) - GET_FILENAME_COMPONENT(_outdir ${_output} PATH ) - - GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE) - _PCH_WRITE_PCHDEP_CXX(${_targetName} ${_input} _pch_dephelp_cxx) - - IF(${_targetType} STREQUAL SHARED_LIBRARY) - ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx} ) - ELSE(${_targetType} STREQUAL SHARED_LIBRARY) - ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx}) - ENDIF(${_targetType} STREQUAL SHARED_LIBRARY) + ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" ) - set_target_properties(${_targetName}_pch_dephelp PROPERTIES - DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/" - ) + set_target_properties(${_targetName}_pch_dephelp PROPERTIES + DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" + ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}" + ) + _PCH_GET_COMPILE_FLAGS(_compile_FLAGS) - FILE(MAKE_DIRECTORY ${_outdir}) + #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}") + #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}") + + ADD_CUSTOM_COMMAND( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}" + COMMAND ${CMAKE_COMMAND} -E copy "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc + DEPENDS "${_input}" + ) + #message("_command ${_input} ${_output}") + _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} ) - _PCH_GET_COMPILE_FLAGS(_compile_FLAGS) + GET_FILENAME_COMPONENT(_outdir ${_output} PATH) + ADD_CUSTOM_COMMAND( + OUTPUT "${_output}" + COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}" + COMMAND ${_command} + DEPENDS "${_input}" + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}" + DEPENDS ${_targetName}_pch_dephelp + ) - #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}") - #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}") - SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/${_name} PROPERTIES GENERATED 1) - ADD_CUSTOM_COMMAND( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name} - COMMAND ${CMAKE_COMMAND} -E copy ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} # ensure same directory! Required by gcc - DEPENDS ${_input} - ) - - #message("_command ${_input} ${_output}") - _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} ) - - #message(${_input} ) - #message("_output ${_output}") - - ADD_CUSTOM_COMMAND( - OUTPUT ${_output} - COMMAND ${_command} - DEPENDS ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp - ) - - - ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn}) + ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn}) + ENDMACRO(ADD_PRECOMPILED_HEADER) @@ -253,72 +241,66 @@ ENDMACRO(ADD_PRECOMPILED_HEADER) MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input) - if(CMAKE_GENERATOR MATCHES Visual*) + if(CMAKE_GENERATOR MATCHES "^Visual.*$") + SET(_dummy_str "#include \"${_input}\"\n" +"// This is required to suppress LNK4221. Very annoying.\n" +"void *g_${_targetName}Dummy = 0\;\n") - SET(_dummy_str "#include \"${_input}\"\n" - "// This is required to suppress LNK4221. Very annoying.\n" - "void *g_${_targetName}Dummy = 0\;\n") - - # Use of cxx extension for generated files (as Qt does) - SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx) - if(EXISTS ${${_targetName}_pch}) - # Check if contents is the same, if not rewrite - # todo - else(EXISTS ${${_targetName}_pch}) - FILE(WRITE ${${_targetName}_pch} ${_dummy_str}) - endif(EXISTS ${${_targetName}_pch}) - endif(CMAKE_GENERATOR MATCHES Visual*) + # Use of cxx extension for generated files (as Qt does) + SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx) + if(EXISTS ${${_targetName}_pch}) + # Check if contents is the same, if not rewrite + # todo + else() + FILE(WRITE ${${_targetName}_pch} ${_dummy_str}) + endif() + endif() ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER) MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input) - IF( "${ARGN}" STREQUAL "0") - SET(_dowarn 0) - ELSE( "${ARGN}" STREQUAL "0") - SET(_dowarn 1) - ENDIF("${ARGN}" STREQUAL "0") + IF(ARGN STREQUAL "0") + SET(_dowarn 0) + ELSE() + SET(_dowarn 1) + ENDIF() - if(CMAKE_GENERATOR MATCHES Visual*) - # Auto include the precompile (useful for moc processing, since the use of - # precompiled is specified at the target level - # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt) + if(CMAKE_GENERATOR MATCHES "^Visual.*$") + + # Auto include the precompile (useful for moc processing, since the use of + # precompiled is specified at the target level + # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt) - GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS) - if (${oldProps} MATCHES NOTFOUND) - SET(oldProps "") - endif(${oldProps} MATCHES NOTFOUND) + GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS) + if (oldProps MATCHES NOTFOUND) + SET(oldProps "") + endif() - SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"") - SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}") + SET(newProperties "${oldProps} /Yu\"${_input}\" /FI\"${_input}\"") + SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "${newProperties}") - #also inlude ${oldProps} to have the same compile options - SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"") + #also inlude ${oldProps} to have the same compile options + SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"") - else(CMAKE_GENERATOR MATCHES Visual*) + elseif (CMAKE_GENERATOR MATCHES Xcode) + + # For Xcode, cmake needs my patch to process + # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties - if (CMAKE_GENERATOR MATCHES Xcode) - # For Xcode, cmake needs my patch to process - # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties + # When buiding out of the tree, precompiled may not be located + # Use full path instead. + GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE) - GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS) - if (${oldProps} MATCHES NOTFOUND) - SET(oldProps "") - endif(${oldProps} MATCHES NOTFOUND) + SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}") + SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES") - # When buiding out of the tree, precompiled may not be located - # Use full path instead. - GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE) + else() - SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}") - SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES") + #Fallback to the "old" precompiled suppport + #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn}) - else (CMAKE_GENERATOR MATCHES Xcode) - - #Fallback to the "old" precompiled suppport - #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn}) - endif(CMAKE_GENERATOR MATCHES Xcode) - endif(CMAKE_GENERATOR MATCHES Visual*) + endif() ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER) diff --git a/modules/gpu/CMakeLists.txt b/modules/gpu/CMakeLists.txt index 19fae4608d..84dfb856c3 100644 --- a/modules/gpu/CMakeLists.txt +++ b/modules/gpu/CMakeLists.txt @@ -6,8 +6,8 @@ project(${the_target}) set(DEPS "opencv_core" "opencv_imgproc" "opencv_objdetect" "opencv_features2d" "opencv_flann" "opencv_calib3d") #"opencv_features2d" "opencv_flann" "opencv_objdetect" - only headers needed set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} opencv_gpu) -include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include" - "${CMAKE_CURRENT_SOURCE_DIR}/src/cuda" +include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include" + "${CMAKE_CURRENT_SOURCE_DIR}/src/cuda" "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_BINARY_DIR}") @@ -85,18 +85,6 @@ endforeach() add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${lib_device_hdrs} ${ncv_srcs} ${ncv_hdrs} ${ncv_cuda} ${cuda_objs}) -if(PCHSupport_FOUND) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_target}_pch "src/precomp.cpp") - endif() - add_native_precompiled_header(${the_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_target} ${pch_header}) - endif() -endif() - # For dynamic link numbering convenions set_target_properties(${the_target} PROPERTIES VERSION ${OPENCV_VERSION} @@ -112,36 +100,38 @@ if (BUILD_SHARED_LIBS) if (MSVC) set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS) else() - add_definitions(-DCVAPI_EXPORTS) - endif() + add_definitions(-DCVAPI_EXPORTS) + endif() endif() +add_opencv_precompiled_headers(${the_target}) + # Additional target properties set_target_properties(${the_target} PROPERTIES - DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" + DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} INSTALL_NAME_DIR lib - ) + ) # Add the required libraries for linking: target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${DEPS} ) if (HAVE_CUDA) - target_link_libraries(${the_target} ${CUDA_LIBRARIES}) + target_link_libraries(${the_target} ${CUDA_LIBRARIES}) CUDA_ADD_CUFFT_TO_TARGET(${the_target}) - - unset(CUDA_npp_LIBRARY CACHE) - find_cuda_helper_libs(npp) - target_link_libraries(${the_target} ${CUDA_npp_LIBRARY}) + + unset(CUDA_npp_LIBRARY CACHE) + find_cuda_helper_libs(npp) + target_link_libraries(${the_target} ${CUDA_npp_LIBRARY}) endif() if(MSVC) - if(CMAKE_CROSSCOMPILING) - set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk") - endif() - - set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc") + if(CMAKE_CROSSCOMPILING) + set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk") + endif() + + set_target_properties(${the_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc") endif() # Dependencies of this target: @@ -181,11 +171,8 @@ if(BUILD_TESTS AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test) set(test_deps opencv_${name} opencv_ts opencv_highgui opencv_calib3d ${DEPS}) foreach(d ${test_deps}) if(${d} MATCHES "opencv_") - if(${d} MATCHES "opencv_lapack") - else() - string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) - include_directories("${d_dir}/include") - endif() + string(REPLACE "opencv_" "${CMAKE_CURRENT_SOURCE_DIR}/../" d_dir ${d}) + include_directories("${d_dir}/include") endif() endforeach() @@ -203,17 +190,7 @@ if(BUILD_TESTS AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test) add_executable(${the_test_target} ${test_srcs} ${test_hdrs} ${nvidia}) - if(PCHSupport_FOUND) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/test/test_precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_test_target}_pch "test/test_precomp.cpp") - endif() - add_native_precompiled_header(${the_test_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_test_target} ${pch_header}) - endif() - endif() + add_opencv_precompiled_headers(${the_test_target}) # Additional target properties set_target_properties(${the_test_target} PROPERTIES @@ -223,7 +200,7 @@ if(BUILD_TESTS AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test) if(ENABLE_SOLUTION_FOLDERS) set_target_properties(${the_test_target} PROPERTIES FOLDER "tests") - endif() + endif() add_dependencies(${the_test_target} ${test_deps}) diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt index 62d2216423..765132a979 100644 --- a/modules/highgui/CMakeLists.txt +++ b/modules/highgui/CMakeLists.txt @@ -263,21 +263,11 @@ if (BUILD_SHARED_LIBS) if(MSVC) set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS) else() - add_definitions(-DCVAPI_EXPORTS) + add_definitions(-DCVAPI_EXPORTS) endif() endif() -if(PCHSupport_FOUND AND USE_PRECOMPILED_HEADERS) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_target}_pch "src/precomp.cpp") - endif() - add_native_precompiled_header(${the_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_target} ${pch_header}) - endif() -endif() +add_opencv_precompiled_headers(${the_target}) # For dynamic link numbering convenions if(NOT ANDROID) @@ -288,7 +278,7 @@ if(NOT ANDROID) ) endif() -set_target_properties(${the_target} PROPERTIES OUTPUT_NAME "${the_target}${OPENCV_DLLVERSION}" ) +set_target_properties(${the_target} PROPERTIES OUTPUT_NAME "${the_target}${OPENCV_DLLVERSION}" ) # Additional target properties set_target_properties(${the_target} PROPERTIES @@ -398,27 +388,17 @@ if(BUILD_TESTS) add_executable(${the_target} ${test_srcs} ${test_hdrs}) - if(PCHSupport_FOUND AND USE_PRECOMPILED_HEADERS) - set(pch_header ${CMAKE_CURRENT_SOURCE_DIR}/test/test_precomp.hpp) - if(${CMAKE_GENERATOR} MATCHES "Visual*" OR ${CMAKE_GENERATOR} MATCHES "Xcode*") - if(${CMAKE_GENERATOR} MATCHES "Visual*") - set(${the_target}_pch "test/test_precomp.cpp") - endif() - add_native_precompiled_header(${the_target} ${pch_header}) - elseif(CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_GENERATOR} MATCHES ".*Makefiles") - add_precompiled_header(${the_target} ${pch_header}) - endif() - endif() + add_opencv_precompiled_headers(${the_target}) # Additional target properties set_target_properties(${the_target} PROPERTIES DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}" ) - + if(ENABLE_SOLUTION_FOLDERS) - set_target_properties(${the_target} PROPERTIES FOLDER "tests") - endif() + set_target_properties(${the_target} PROPERTIES FOLDER "tests") + endif() add_dependencies(${the_target} ${test_deps})