From e28c988a04c15c2cedfe7e069a066463f4e4c452 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 23 Aug 2016 11:37:53 +0200 Subject: cmake: refactor boilerplate for projects Don't rely on a macro that shouldn't be necessary. The information "opentrack_boilerplate" retrieves is now factored out to be easily available to projects themselves. opentrack_boilerplate can now also build executables. When appropriate, target properties are now concatenated rather than replaced. --- CMakeLists.txt | 3 + cmake/opentrack-boilerplate.cmake | 213 ++++++++++++++++++++++++-------------- cmake/opentrack-policy.cmake | 15 ++- cmake/opentrack-version.cmake | 2 +- compat/CMakeLists.txt | 5 +- gui/CMakeLists.txt | 17 ++- proto-fsuipc/CMakeLists.txt | 2 +- proto-wine/CMakeLists.txt | 5 +- qxt-mini/CMakeLists.txt | 3 +- x-plane-plugin/CMakeLists.txt | 33 +++--- 10 files changed, 179 insertions(+), 119 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 25220fe5..8fadd0f6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,9 @@ file(GLOB opentrack-subprojects "macosx/${C}" "cv/${C}" ) + +list(SORT opentrack-subprojects) + foreach(i ${opentrack-subprojects}) get_filename_component(i ${i} DIRECTORY) add_subdirectory(${i}) diff --git a/cmake/opentrack-boilerplate.cmake b/cmake/opentrack-boilerplate.cmake index d00a4a75..5cabe949 100644 --- a/cmake/opentrack-boilerplate.cmake +++ b/cmake/opentrack-boilerplate.cmake @@ -29,120 +29,177 @@ install(DIRECTORY "${CMAKE_SOURCE_DIR}/bin" DESTINATION "${opentrack-doc-src-pfx install(FILES "${CMAKE_SOURCE_DIR}/README.md" DESTINATION "${opentrack-doc-src-pfx}") install(FILES "${CMAKE_SOURCE_DIR}/CONTRIBUTING.md" DESTINATION "${opentrack-doc-src-pfx}") -function(opentrack_set_globs n) - set(dir ${PROJECT_SOURCE_DIR}) - file(GLOB ${n}-c ${dir}/*.cpp ${dir}/*.c ${dir}/*.h ${dir}/*.hpp) - file(GLOB ${n}-res ${dir}/*.rc) - foreach(f ${n}-res) +function(opentrack_glob_sources var) + set(dir "${CMAKE_CURRENT_SOURCE_DIR}") + file(GLOB ${var}-c ${dir}/*.cpp ${dir}/*.c ${dir}/*.h ${dir}/*.hpp) + file(GLOB ${var}-res ${dir}/*.rc) + foreach(f ${var}-res) set_source_files_properties(${f} PROPERTIES LANGUAGE RC) endforeach() - file(GLOB ${n}-ui ${dir}/*.ui) - file(GLOB ${n}-rc ${dir}/*.qrc) - foreach(i c res ui rc) - set(${n}-${i} ${${n}-${i}} PARENT_SCOPE) + file(GLOB ${var}-ui ${dir}/*.ui) + file(GLOB ${var}-rc ${dir}/*.qrc) + file(GLOB aa ${dir}/*.qrc) + set(${var}-all ${${var}-c} ${${var}-rc} ${${var}-rcc} ${${var}-uih} ${${var}-moc} ${${var}-res}) + foreach(i ui rc res c all) + set(${var}-${i} "${${var}-${i}}" PARENT_SCOPE) endforeach() endfunction() function(opentrack_qt n) qt5_wrap_cpp(${n}-moc ${${n}-c} OPTIONS --no-notes) - QT5_WRAP_UI(${n}-uih ${${n}-ui}) - QT5_ADD_RESOURCES(${n}-rcc ${${n}-rc}) - set(${n}-all ${${n}-c} ${${n}-rc} ${${n}-rcc} ${${n}-uih} ${${n}-moc} ${${n}-res}) - foreach(i moc uih rcc all) - set(${n}-${i} ${${n}-${i}} PARENT_SCOPE) + qt5_wrap_ui(${n}-uih ${${n}-ui}) + qt5_add_resources(${n}-rcc ${${n}-rc}) + foreach(i moc uih rcc) + set(${n}-${i} "${${n}-${i}}" PARENT_SCOPE) + list(APPEND ${n}-all ${${n}-${i}}) endforeach() + set(${n}-all "${${n}-all}" PARENT_SCOPE) endfunction() -function(opentrack_compat target) +function(opentrack_fixup_subsystem n) if(MSVC) - set(msvc-subsystem "/VERSION:5.1 /SUBSYSTEM:WINDOWS,5.01") - set_target_properties(${target} PROPERTIES LINK_FLAGS "${msvc-subsystem} /DEBUG /OPT:ICF") + if(SDK_CONSOLE_DEBUG) + set(subsystem CONSOLE) + else() + set(subsystem WINDOWS) + endif() + if("$ENV{LIBPATH}" STREQUAL "") + message(FATAL_ERROR "no vcvars.bat run; probably no editbin in PATH") + endif() + set(loc "$") + add_custom_command(TARGET "${n}" + POST_BUILD + COMMAND editbin -nologo -SUBSYSTEM:${subsystem},5.01 -OSVERSION:5.1 \"${loc}\" + COMMENT "Fixing up Windows XP support for target ${n}") endif() +endfunction() + +function(opentrack_compat target) if(NOT MSVC) set_property(SOURCE ${${target}-moc} APPEND_STRING PROPERTY COMPILE_FLAGS "-w -Wno-error") endif() if(WIN32) target_link_libraries(${target} dinput8 dxguid strmiids) endif() + opentrack_fixup_subsystem(${target}) +endfunction() + +function(opentrack_sources n ret) + get_target_property(srcs ${n} SOURCES) + if(srcs) + foreach(f ${srcs}) + get_source_file_property(autogen "${f}" GENERATED) + if(NOT autogen) + list(APPEND ${ret} "${f}") + endif() + endforeach() + endif() + set(${ret} "${${ret}}" PARENT_SCOPE) endfunction() include(CMakeParseArguments) -function(opentrack_boilerplate__ n files_ no-library_ static_ no-compat_ compile_ link_ stage2_ bin_) - if((NOT no-library_) AND (NOT stage2_)) +function(opentrack_install_sources n) + opentrack_sources(${n} sources) + file(RELATIVE_PATH subdir "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}") + foreach (i ${sources}) + install(FILES "${i}" DESTINATION "${opentrack-doc-src-pfx}/${subdir}") + endforeach() + install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt" DESTINATION "${opentrack-doc-src-pfx}/${subdir}") +endfunction() + +function(opentrack_is_target_c_only ret srcs) + set(val TRUE) + foreach(i ${srcs}) + get_filename_component(ext "${i}" EXT) + string(TOLOWER "${ext}" ext) + if(ext STREQUAL ".cpp") + set(val FALSE) + break() + endif() + endforeach() + set(${ret} "${val}" PARENT_SCOPE) +endfunction() + +function(opentrack_boilerplate n) + message(STATUS "module ${n}") + cmake_parse_arguments(arg + "STATIC;NO-COMPAT;BIN;EXECUTABLE;NO-QT;WIN32-CONSOLE" + "LINK;COMPILE" + "" + ${ARGN} + ) + if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") + message(FATAL_ERROR "opentrack_boilerplate bad formals: ${arg_UNPARSED_ARGUMENTS}") + endif() + + project(${n}) + opentrack_glob_sources(${n}) + opentrack_is_target_c_only(is-c-only "${${n}-all}") + if(NOT is-c-only) + opentrack_qt(${n}) + endif() + + if(arg_NO-QT) + set(arg_NO-COMPAT TRUE) + endif() + + string(TOUPPER arg_WIN32-CONSOLE "${arg_WIN32-CONSOLE}") + + if(NOT WIN32) + set(subsys "") + elseif(MSVC) + set(subsys "WIN32") + elseif(arg_WIN32-CONSOLE) + set(subsys "") + else() + set(subsys "WIN32") + endif() + + if(arg_EXECUTABLE) + add_executable(${n} ${subsys} "${${n}-all}") + else() set(link-mode SHARED) - if (static_) + if (arg_STATIC) set(link-mode STATIC) endif() - add_library(${n} ${link-mode} ${files_}) - set(all-files ${${n}-c} ${${n}-res} ${${n}-ui} ${${n}-rc}) - file(RELATIVE_PATH subdir ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) - foreach (i ${all-files}) - install(FILES "${i}" DESTINATION "${opentrack-doc-src-pfx}/${subdir}") - endforeach() - install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt" DESTINATION "${opentrack-doc-src-pfx}/${subdir}") - message(STATUS "module ${n}") + add_library(${n} ${link-mode} "${${n}-all}") endif() - if(NOT no-library_) - opentrack_compat(${n}) + + if(NOT arg_NO-QT) + target_link_libraries(${n} ${MY_QT_LIBS}) endif() - if(NOT no-compat_) + + if(NOT arg_NO-COMPAT) target_link_libraries(${n} opentrack-api opentrack-options opentrack-compat) endif() - target_link_libraries(${n} ${MY_QT_LIBS}) - if(CMAKE_COMPILER_IS_GNUCXX) - set(c-props "-fvisibility=hidden -fuse-cxa-atexit") + + opentrack_install_sources(${n}) + opentrack_compat(${n}) + + if(CMAKE_COMPILER_IS_GNUCXX OR APPLE) + set(c-props "-fvisibility=hidden") + if(NOT is-c-only) + set(c-props "${c-props} -fuse-cxa-atexit") + endif() endif() if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE) set(l-props "-Wl,--as-needed") endif() - get_target_property(orig-compile ${n} COMPILE_FLAGS) - if(NOT orig-compile) - set(orig-compile "") - endif() - get_target_property(orig-link ${n} LINK_FLAGS) - if(NOT orig-link) - set(orig-link "") - endif() - set_target_properties(${n} PROPERTIES - COMPILE_FLAGS "${c-props} ${orig-compile} ${compile_}" - LINK_FLAGS "${l-props} ${orig-link} ${link_}" - ) - string(REGEX REPLACE "^opentrack-" "" n_ ${n}) - string(REPLACE "-" "_" n_ ${n_}) - target_compile_definitions(${n} PRIVATE "BUILD_${n_}") - if((NOT static_) AND (NOT no-library_)) - if(bin_ AND WIN32) + + set_property(TARGET ${n} APPEND_STRING PROPERTY COMPILE_FLAGS "${c-props} ${arg_COMPILE}") + set_property(TARGET ${n} APPEND_STRING PROPERTY LINK_FLAGS "${l-props} ${arg_LINK}") + + if(NOT arg_STATIC) + string(REGEX REPLACE "^opentrack-" "" n_ "${n}") + string(REGEX REPLACE "^(tracker|filter-proto)-" "" n_ "${n_}") + string(REPLACE "-" "_" n_ ${n_}) + target_compile_definitions(${n} PRIVATE "BUILD_${n_}") + + if(arg_BIN AND WIN32) install(TARGETS ${n} RUNTIME DESTINATION . LIBRARY DESTINATION .) else() install(TARGETS ${n} ${opentrack-hier-str}) endif() endif() endfunction() - -macro(opentrack_boilerplate n) - cmake_parse_arguments(${n}-args - "NO-LIBRARY;STATIC;NO-COMPAT;STAGE2;BIN" - "LINK;COMPILE" - "" - ${ARGN} - ) - if(NOT "${${n}-args_UNPARSED_ARGUMENTS}" STREQUAL "") - message(FATAL_ERROR "opentrack_boilerplate bad formals ${${n}-args_UNPARSED_ARGUMENTS}") - endif() - if(NOT ${n}-args_STAGE2) - project(${n} C CXX) - opentrack_set_globs(${n}) - opentrack_qt(${n}) - endif() - if((NOT ${n}-args_NO-LIBRARY) OR ${n}-args_STAGE2) - opentrack_boilerplate__("${n}" "${${n}-all}" - "${${n}-args_NO-LIBRARY}" - "${${n}-args_STATIC}" - "${${n}-args_NO-COMPAT}" - "${${n}-args_COMPILE}" - "${${n}-args_LINK}" - "${${n}-args_STAGE2}" - "${${n}-args_BIN}") - endif() -endmacro() diff --git a/cmake/opentrack-policy.cmake b/cmake/opentrack-policy.cmake index 48aba3cc..6416c535 100644 --- a/cmake/opentrack-policy.cmake +++ b/cmake/opentrack-policy.cmake @@ -1,10 +1,19 @@ -cmake_policy(SET CMP0020 NEW) +if(POLICY CMP0020) + cmake_policy(SET CMP0020 NEW) +endif() + if(POLICY CMP0058) - cmake_policy(SET CMP0058 OLD) + cmake_policy(SET CMP0058 NEW) endif() + if(POLICY CMP0028) - cmake_policy(SET CMP0028 OLD) + cmake_policy(SET CMP0028 NEW) endif() + if(POLICY CMP0042) cmake_policy(SET CMP0042 NEW) endif() + +if(POLICY CMP0063) + cmake_policy(SET CMP0063 NEW) +endif() diff --git a/cmake/opentrack-version.cmake b/cmake/opentrack-version.cmake index a72347ee..8e6dc1c8 100644 --- a/cmake/opentrack-version.cmake +++ b/cmake/opentrack-version.cmake @@ -43,6 +43,6 @@ endif() add_library(opentrack-version STATIC ${CMAKE_BINARY_DIR}/version.c) if(NOT MSVC) - SET_TARGET_PROPERTIES(opentrack-version PROPERTIES COMPILE_FLAGS "-fno-lto") + set_property(TARGET opentrack-version APPEND_STRING PROPERTY COMPILE_FLAGS "-fno-lto") endif() opentrack_compat(opentrack-version) diff --git a/compat/CMakeLists.txt b/compat/CMakeLists.txt index 2bbb496c..7404bdfb 100644 --- a/compat/CMakeLists.txt +++ b/compat/CMakeLists.txt @@ -1,8 +1,9 @@ opentrack_boilerplate(opentrack-compat NO-COMPAT BIN) + if(NOT WIN32 AND NOT APPLE) target_link_libraries(opentrack-compat rt) endif() + if(CMAKE_COMPILER_IS_GNUCXX) - set_source_files_properties(nan.cpp PROPERTIES - COMPILE_FLAGS "-fno-fast-math -fno-finite-math-only -O0") + set_property(SOURCE nan.cpp APPEND_STRING PROPERTY COMPILE_FLAGS "-fno-lto -fno-fast-math -fno-finite-math-only -O0") endif() diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 6fa8d034..90d1c34f 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -1,16 +1,14 @@ -opentrack_boilerplate(opentrack NO-LIBRARY) +if(MSVC OR (NOT SDK_CONSOLE_DEBUG AND WIN32)) + set(console "") +else() + set(console WIN32-CONSOLE) +endif() if(WIN32) SET(SDK_CONSOLE_DEBUG FALSE CACHE BOOL "Console window visible at runtime") endif() -if(WIN32 AND NOT SDK_CONSOLE_DEBUG) - set(opentrack-win32-executable WIN32) -else() - set(opentrack-win32-executable "") -endif() - -add_executable(opentrack ${opentrack-win32-executable} ${opentrack-all}) +opentrack_boilerplate(opentrack EXECUTABLE ${console}) set_target_properties(opentrack PROPERTIES SUFFIX "${opentrack-binary-suffix}") @@ -31,6 +29,3 @@ if(LINUX) # for process detector target_link_libraries(opentrack procps) endif() - -opentrack_boilerplate(opentrack STAGE2 BIN) - diff --git a/proto-fsuipc/CMakeLists.txt b/proto-fsuipc/CMakeLists.txt index ec897ae8..57da4b1a 100644 --- a/proto-fsuipc/CMakeLists.txt +++ b/proto-fsuipc/CMakeLists.txt @@ -5,7 +5,7 @@ if(WIN32) target_link_libraries(opentrack-proto-fsuipc ${SDK_FSUIPC}/FSUIPC_User.lib) target_include_directories(opentrack-proto-fsuipc SYSTEM PUBLIC ${SDK_FSUIPC}) if(MSVC) - set_target_properties(opentrack-proto-fsuipc PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBC.lib") + set_property(TARGET opentrack-proto-fsuipc APPEND_STRING PROPERTY LINK_FLAGS "/NODEFAULTLIB:LIBC.lib") endif() endif() endif() diff --git a/proto-wine/CMakeLists.txt b/proto-wine/CMakeLists.txt index f949d1ad..780b1e16 100644 --- a/proto-wine/CMakeLists.txt +++ b/proto-wine/CMakeLists.txt @@ -3,13 +3,13 @@ if(NOT WIN32) set(SDK_WINE_NO_WRAPPER FALSE CACHE BOOL "disable Wine wrapper -- use Wine only for X-Plane") if(SDK_WINE_PREFIX) opentrack_boilerplate(opentrack-proto-wine) - opentrack_link(opentrack-proto-wine opentrack-compat opentrack-csv) + target_link_libraries(opentrack-proto-wine opentrack-csv) if(NOT SDK_WINE_NO_WRAPPER) set(my-rt -lrt) if(APPLE) set(my-rt) endif() - file(GLOB wine-deps ${PROJECT_SOURCE_DIR}/*.cxx) + file(GLOB wine-deps "${CMAKE_CURRENT_SOURCE_DIR}/*.cxx") install(FILES ${wine-deps} DESTINATION "${opentrack-doc-src-pfx}/proto-wine") add_custom_command( OUTPUT opentrack-wrapper-wine.exe.so @@ -24,5 +24,4 @@ if(NOT WIN32) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/opentrack-wrapper-wine.exe.so" DESTINATION ${opentrack-hier-pfx}) endif() endif() - opentrack_register(opentrack-proto-wine) endif() diff --git a/qxt-mini/CMakeLists.txt b/qxt-mini/CMakeLists.txt index 020788ec..6b71c500 100644 --- a/qxt-mini/CMakeLists.txt +++ b/qxt-mini/CMakeLists.txt @@ -3,7 +3,6 @@ if(UNIX OR APPLE) if(NOT APPLE) target_link_libraries(opentrack-qxt-mini X11) else() - set_target_properties(opentrack-qxt-mini PROPERTIES LINK_FLAGS - "-framework Carbon -framework CoreFoundation") + set_property(TARGET opentrack-qxt-mini APPEND_STRING PROPERTY LINK_FLAGS "-framework Carbon -framework CoreFoundation") endif() endif() diff --git a/x-plane-plugin/CMakeLists.txt b/x-plane-plugin/CMakeLists.txt index 616f60ee..de17b60d 100644 --- a/x-plane-plugin/CMakeLists.txt +++ b/x-plane-plugin/CMakeLists.txt @@ -1,31 +1,28 @@ - if(LINUX OR APPLE) - opentrack_boilerplate(opentrack-xplane-plugin NO-LIBRARY) set(SDK_XPLANE "" CACHE PATH "Path to X-Plane SDK") + opentrack_boilerplate(opentrack-xplane-plugin NO-QT) + if(SDK_XPLANE) # probably librt already included - add_library(opentrack-xplane-plugin SHARED ${opentrack-xplane-plugin-c}) install(FILES ${opentrack-xplane-plugin-c} DESTINATION "${opentrack-doc-src-pfx}/opentrack-xplane-plugin") target_include_directories(opentrack-xplane-plugin SYSTEM PUBLIC ${SDK_XPLANE}/CHeaders ${SDK_XPLANE}/CHeaders/XPLM) - set(vis "-fvisibility=hidden") + set(begin TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY) + if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE) - SET_TARGET_PROPERTIES(opentrack-xplane-plugin - PROPERTIES LINK_FLAGS - "-shared -rdynamic -nodefaultlibs -undefined_warning -fPIC" - COMPILE_FLAGS "${vis} -Wall -O2 -pipe -fPIC -DLIN -DXPLM200 -DXPLM210" - LIBRARY_OUTPUT_NAME "opentrack.xpl" - PREFIX "" SUFFIX "") - endif() - if(APPLE) - SET_TARGET_PROPERTIES(opentrack-xplane-plugin PROPERTIES - COMPILE_FLAGS "${vis} -iframework ${SDK_XPLANE}/Libraries/Mac/ -DAPL -DXPLM200 -DXPLM210 -framework XPLM -framework XPWidgets" - LINK_FLAGS "-F${SDK_XPLANE}/Libraries/Mac/ -framework XPLM -framework XPWidgets" - LIBRARY_OUTPUT_NAME "opentrack.xpl" - PREFIX "" SUFFIX "") + set_property(${begin} COMPILE_FLAGS "-O2 -pipe -fPIC -DLIN -DXPLM200 -DXPLM210") + set_property(${begin} LINK_FLAGS "-shared -rdynamic -nodefaultlibs -undefined_warning -fPIC") + elseif(APPLE) + set_property(${begin} COMPILE_FLAGS "-iframework ${SDK_XPLANE}/Libraries/Mac/ -DAPL -DXPLM200 -DXPLM210 -framework XPLM -framework XPWidgets") + set_property(${begin} LINK_FLAGS "-F${SDK_XPLANE}/Libraries/Mac/ -framework XPLM -framework XPWidgets") endif() + + set_target_properties(opentrack-xplane-plugin PROPERTIES + LIBRARY_OUTPUT_NAME "opentrack.xpl" + PREFIX "" + SUFFIX "") + if(UNIX AND NOT APPLE) target_link_libraries(opentrack-xplane-plugin rt) endif() - install(TARGETS opentrack-xplane-plugin RUNTIME DESTINATION ${opentrack-hier-pfx} LIBRARY DESTINATION ${opentrack-hier-pfx} ) endif() endif() -- cgit v1.2.3