Remove duplication in version info target creation
authorTeemu Murtola <teemu.murtola@gmail.com>
Sat, 20 Sep 2014 04:14:02 +0000 (07:14 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sat, 20 Sep 2014 17:34:17 +0000 (19:34 +0200)
Now the custom target created for the git version info generation uses
gmx_add_custom_output_target(), removing comments etc. that were
duplicated in both places and clarifying the code.  The custom target
created with gmx_configure_version_file() now also integrates with the
dependency resolution mechanism used by gmx_add_custom_output_target().

Change-Id: I868264498c014041f551df8819fb2b568d85bc30

cmake/gmxCustomCommandUtilities.cmake
cmake/gmxVersionInfo.cmake

index 902fa23ee365b3a4667bb7187ee939b7841dc0f4..3a13396e628fe1f496b5707967c3c254c1d2bea4 100644 (file)
@@ -65,6 +65,29 @@ function (gmx_get_stamp_filename variable targetname)
         PARENT_SCOPE)
 endfunction()
 
+# Helper function to tell gmx_add_custom_output_target() the name of an output
+# file for a custom target.
+#
+# Usage:
+#   gmx_set_custom_target_output(<targetname> <output>)
+#
+#   <targetname> - name of an existing custom target
+#   <output>     - path to the output file produced by the custom target
+#
+# This is used internally by gmx_add_custom_output_target(), but can also be
+# called for targets created with add_custom_target() to make them work with
+# the dependency resolution mechanism in gmx_add_custom_output_target() if
+# those targets for some reason cannot be created with that command.
+function (gmx_set_custom_target_output targetname output)
+    # Store the output file name in a custom property to be used in dependency
+    # resolution later.
+    if (NOT IS_ABSOLUTE ${output})
+        set(output ${CMAKE_CURRENT_BINARY_DIR}/${output})
+    endif()
+    set_property(TARGET ${targetname}
+        PROPERTY GMX_CUSTOM_TARGET_OUTPUT_FILE ${output})
+endfunction()
+
 # More flexible alternative to add_custom_command() and add_custom_target()
 # for dependent custom commands.  It adds a few convenience features:
 #   - Support for custom commands that always run (like add_custom_target()),
@@ -225,11 +248,7 @@ function (gmx_add_custom_output_target targetname)
     endif()
     # Store the output file name in a custom property to be used in dependency
     # resolution later.
-    if (NOT IS_ABSOLUTE ${_output})
-        set(_output ${CMAKE_CURRENT_BINARY_DIR}/${_output})
-    endif()
-    set_property(TARGET ${targetname}
-        PROPERTY GMX_CUSTOM_TARGET_OUTPUT_FILE ${_output})
+    gmx_set_custom_target_output(${targetname} ${_output})
     # Create the fast target if requested.
     if (_add_fast)
         add_custom_target(${targetname}-fast ${_command_args} VERBATIM)
index 7b8d08402f9374a98f8a63bb896650140d1b88fc..6430211f29ef31c8f5be264db98ab49345e91246 100644 (file)
@@ -256,6 +256,8 @@ if (GMX_GIT_VERSION_INFO)
     endif()
 endif()
 
+include(gmxCustomCommandUtilities)
+
 # The first two are also for use outside this file, encapsulating the details
 # of how to use the generated VersionInfo.cmake.
 set(VERSION_INFO_CMAKE_FILE   ${PROJECT_BINARY_DIR}/VersionInfo.cmake)
@@ -310,52 +312,17 @@ if (GMX_GIT_VERSION_INFO)
     # All targets added by gmx_configure_version_file() use the information
     # from this script to get their variables from, removing the need to run
     # git multiple times and simplifying reuse for other purposes.
-    #
-    # Ninja requires all generated files mentioned in dependencies of custom
-    # commands (in gmx_configure_version_info()) to be actually mentioned in
-    # the build system, and luckily add_custom_command() makes that possible.
-    # But it seems impossible to create a robust custom command that would be
-    # always run, so other generators that do not have this constraint simply
-    # use an add_custom_target().
-    if (CMAKE_GENERATOR STREQUAL "Ninja")
-        # The second, phony file is never created, so the rule is always
-        # triggered again.  TODO: Figure out why this works, even though ninja
-        # very eagerly complains about missing files.
-        # This unfortunately does not work with the make generator, as
-        # the non-existent second file causes some part of the generated system
-        # erase the first file at the beginning of every build, causing a full
-        # rebuild of the dependencies.
-        add_custom_command(OUTPUT ${VERSION_INFO_CMAKE_FILE} git-version-phony
-            COMMAND ${CMAKE_COMMAND}
-                -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-                -D PROJECT_VERSION=${GMX_VERSION_STRING}
-                -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
-                -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
-                -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
-                -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfo.cmake
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-            COMMENT "Generating git version information"
-            VERBATIM)
-        # The generated Ninja build system would probably work fine even
-        # without this target, but CMake requires all custom commands to belong
-        # to a target in the same CMakeLists.txt to generate anything for them.
-        add_custom_target(git-version-info DEPENDS ${VERSION_INFO_CMAKE_FILE})
-    else()
-        # For other generators, a target-level dependency on git-version-info
-        # ensures that VERSION_INFO_CMAKE_FILE is created before the dependent
-        # target's dependencies are even evaluated.
-        add_custom_target(git-version-info
-            COMMAND ${CMAKE_COMMAND}
-                -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-                -D PROJECT_VERSION=${GMX_VERSION_STRING}
-                -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
-                -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
-                -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
-                -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfo.cmake
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-            COMMENT "Generating git version information"
-            VERBATIM)
-    endif()
+    gmx_add_custom_output_target(git-version-info RUN_ALWAYS
+        OUTPUT ${VERSION_INFO_CMAKE_FILE}
+        COMMAND ${CMAKE_COMMAND}
+            -D GIT_EXECUTABLE=${GIT_EXECUTABLE}
+            -D PROJECT_VERSION=${GMX_VERSION_STRING}
+            -D PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}
+            -D VERSION_CMAKEIN=${VERSION_INFO_CMAKEIN_FILE_PARTIAL}
+            -D VERSION_OUT=${VERSION_INFO_CMAKE_FILE}
+            -P ${CMAKE_CURRENT_LIST_DIR}/gmxGenerateVersionInfo.cmake
+        WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+        COMMENT "Generating git version information")
     list(APPEND VERSION_INFO_DEPS git-version-info)
 else()
     # If the version info is static, just generate the CMake script with the
@@ -405,6 +372,7 @@ function (gmx_configure_version_file INFILE OUTFILE)
         VERBATIM)
     if (ARG_TARGET)
         add_custom_target(${ARG_TARGET} DEPENDS ${OUTFILE} VERBATIM)
+        gmx_set_custom_target_output(${ARG_TARGET} ${OUTFILE})
     endif()
     if (ARG_SOURCE_FILE)
         set_source_files_properties(${OUTFILE} PROPERTIES GENERATED true)