smanders / externpro

build external projects with cmake
MIT License
13 stars 12 forks source link

xpVersionLib #388

Closed smanders closed 1 year ago

smanders commented 1 year ago

currently xpGenerateResources() generates resource files: Version.hpp, resource.rc, resource.h https://github.com/smanders/externpro/blob/23.02/modules/xpfunmac.cmake#L1476-L1507

it also populates a list of these generated sources which are manually added to the source list so they are searchable and part of the project so they are able to be brought up in an IDE (Visual Studio)

but the worst part of this is that the executable using these resources needs access to the icon file and the directory where the generated resources are, and this is typically CommonLibs_SOURCE_DIR (for the Vantage.ico) and CMAKE_CURRENT_BINARY_DIR (for Version.hpp)

I've reviewed some code that abuses the access to these directories -- so it's time to lock things down: generate these resources in a VersionLib/ directory and only give access to that directory, and create a ${exe_name}Version INTERFACE library that gives access to VersionLib/ and the icon directory -- this INTERFACE library can also add the generated source, so would no longer need to be manually added to the source list

this should simplify projects requiring VersionLib

diff --git a/vantComm/tool/PntToPntTestTool/CMakeLists.txt b/vantComm/tool/PntToPntTestTool/CMakeLists.txt
index eccea671..903d4d53 100644
--- a/vantComm/tool/PntToPntTestTool/CMakeLists.txt
+++ b/vantComm/tool/PntToPntTestTool/CMakeLists.txt
@@ -1,10 +1,10 @@
 project(PntToPntTestTool VERSION ${CMAKE_PROJECT_VERSION})
 set(exe_name ${PROJECT_NAME})
 #######################################
-set(PACKAGE_START_YEAR 2022)
-xpGenerateResources(cmake/Vantage.ico CMakeGenerated_srcs)
-source_group(CMakeGenerated FILES ${CMakeGenerated_srcs})
-list(APPEND ${exe_name}_srcs ${CMakeGenerated_srcs})
+xpGenerateVersionLib(NAME ${exe_name}Version
+  ICON ${CommonLibraries_SOURCE_DIR}/cmake/Vantage.ico
+  START_YEAR 2022
+  )
 #######################################
 set(exe_srcs
   main.cpp
@@ -13,11 +13,7 @@ source_group("" FILES ${exe_srcs})
 list(APPEND ${exe_name}_srcs ${exe_srcs})
 #######################################
 add_executable(${exe_name} ${${exe_name}_srcs})
-target_include_directories(${exe_name} PRIVATE
-  ${CMAKE_CURRENT_BINARY_DIR} # for Version.hpp
-  ${CommonLibraries_SOURCE_DIR} # for Vantage.ico
-  )
-target_link_libraries(${exe_name} PRIVATE Boost::program_options Revision vantComm)
+target_link_libraries(${exe_name} PRIVATE ${exe_name}Version Boost::program_options Revision vantComm)
 set_property(TARGET ${exe_name} PROPERTY FOLDER ${folder})
 install(TARGETS ${exe_name} DESTINATION bintool COMPONENT tool)
 xpSourceListAppend("${${exe_name}_srcs}")

also, an upcoming release of cmakeify, which will standardize and clean-up our target_include_directories() calls will be fine to ditch the # for Version.hpp and # for Vantage.io comments

smanders commented 1 year ago

also, while looking at the cmake documentation for get_filename_component https://cmake.org/cmake/help/v3.24/command/get_filename_component.html it appears PATH is a legacy alias for DIRECTORY -- so might as well switch some of the externpro cmake (there are still going to be a few patches that use PATH, for now)

smanders commented 1 year ago

turns out the .rc file has to be built as part of the executable (the .rc is for Windows)... can't be only in a VersionLib that is linked in -- so I'm gonna change things up a bit and call the new cmake function xpVersionLib and call it with the target as the first argument and do everything else, like actually creating and using the VersionLib, behind the scenes

so now projects will simplify to the following:

diff --git a/vantComm/tool/PntToPntTestTool/CMakeLists.txt b/vantComm/tool/PntToPntTestTool/CMakeLists.txt
index eccea671..ad3d7046 100644
--- a/vantComm/tool/PntToPntTestTool/CMakeLists.txt
+++ b/vantComm/tool/PntToPntTestTool/CMakeLists.txt
@@ -1,11 +1,6 @@
 project(PntToPntTestTool VERSION ${CMAKE_PROJECT_VERSION})
 set(exe_name ${PROJECT_NAME})
 #######################################
-set(PACKAGE_START_YEAR 2022)
-xpGenerateResources(cmake/Vantage.ico CMakeGenerated_srcs)
-source_group(CMakeGenerated FILES ${CMakeGenerated_srcs})
-list(APPEND ${exe_name}_srcs ${CMakeGenerated_srcs})
-#######################################
 set(exe_srcs
   main.cpp
   )
@@ -13,11 +8,8 @@ source_group("" FILES ${exe_srcs})
 list(APPEND ${exe_name}_srcs ${exe_srcs})
 #######################################
 add_executable(${exe_name} ${${exe_name}_srcs})
-target_include_directories(${exe_name} PRIVATE
-  ${CMAKE_CURRENT_BINARY_DIR} # for Version.hpp
-  ${CommonLibraries_SOURCE_DIR} # for Vantage.ico
-  )
 target_link_libraries(${exe_name} PRIVATE Boost::program_options Revision vantComm)
+xpVersionLib(${exe_name} ICON ${CommonLibraries_SOURCE_DIR}/cmake/Vantage.ico START_YEAR 2022)
 set_property(TARGET ${exe_name} PROPERTY FOLDER ${folder})
 install(TARGETS ${exe_name} DESTINATION bintool COMPONENT tool)
 xpSourceListAppend("${${exe_name}_srcs}")
smanders commented 1 year ago

completed with commits to dev branch referenced above verified with TemplateMicroservice (on Linux and Windows) and other projects that generate resources in various ways