Revision: 75996
          http://sourceforge.net/p/brlcad/code/75996
Author:   starseeker
Date:     2020-06-01 17:04:16 +0000 (Mon, 01 Jun 2020)
Log Message:
-----------
CMake's install() throws a wrinkle into the generator expression usage when
combined with add_custom_command.  Somewhat surprisingly it looks as if it can
work, but requires that the custom command be linked to a custom target that
has ALL set so it gets triggered before install takes place.  This is necessary
because we can't pass generator expression paths into the OUTPUT entry of
add_custom_command, so we can't establish a direct file dependency in the build
process.  Nor (as far as I can tell) can we explicitly set target dependencies
for install rules, so we need the ALL enabled target to hook our custom target
in under the install process.

Until the lack of generator expression support in add_custom_command's OUTPUT
specifiers is addressed (https://gitlab.kitware.com/cmake/cmake/-/issues/12877)
it's probably not worth reengineering the DocBook build to shift us off the
existing mechanism - right now it looks like we would be trading one
functional-but-ackward solution for what would probably end up being another
functional-but-ackward solution.

Modified Paths:
--------------
    brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt
    brlcad/trunk/doc/notes/cmake_paths/test.cmake.in

Modified: brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt   2020-06-01 16:07:05 UTC 
(rev 75995)
+++ brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt   2020-06-01 17:04:16 UTC 
(rev 75996)
@@ -4,6 +4,17 @@
 # Set a non-standard, custom binary output path
 set(BIN_DIR deep/dir/bin)
 
+set(LBIN_DIR "${BIN_DIR}")
+while (NOT "${LBIN_DIR}" STREQUAL "")
+  get_filename_component(LBDIR "${LBIN_DIR}" DIRECTORY)
+  set(LBIN_DIR "${LBDIR}")
+  if ("${RBIN_DIR}" STREQUAL "")
+    set(RBIN_DIR "..")
+  else ("${RBIN_DIR}" STREQUAL "")
+    set(RBIN_DIR "../${RBIN_DIR}")
+  endif ("${RBIN_DIR}" STREQUAL "")
+endwhile (NOT "${LBIN_DIR}" STREQUAL "")
+
 # Use the custom binary output directory to set the standard
 # CMake variables controlling binary output locations
 if(NOT CMAKE_CONFIGURATION_TYPES)
@@ -24,7 +35,8 @@
 add_executable(dir_info dir_info.c)
 
 # Use configure_file to set up a script and define variables that will
-# not change at runtime.
+# not change at runtime.  The script will print some messages and generate
+# a file called "test_file.txt" in a build configuration specific directory
 configure_file(test.cmake.in test.cmake @ONLY)
 
 # Use the generator expression from dir_info to pass the runtime binary
@@ -31,10 +43,44 @@
 # directory information to the script.  The script will then use the BIN_DIR
 # variable holding the subdirectory, defined by configure_file, to deconstruct
 # the runtime binary path and find the runtime root path.
-add_custom_target(dinfo
+#
+# When generating output from the script into the configuration specific
+# directory, we can't directly specify the file in OUTPUT.  (See
+# https://gitlab.kitware.com/cmake/cmake/-/issues/12877) Instead we generate a
+# stamp file, which the associated custom target depends on.  As defined below,
+# "make dinfo" will generate test_file.txt in the desired directory but the
+# build system will have problems when it comes to installing it.
+add_custom_command(
+  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test-${CMAKE_CFG_INTDIR}-stamp"
   COMMAND ${CMAKE_COMMAND} -DEXEC_DIR=$<TARGET_FILE_DIR:dir_info> -P test.cmake
+  COMMAND ${CMAKE_COMMAND} -E touch 
"${CMAKE_CURRENT_BINARY_DIR}/test-${CMAKE_CFG_INTDIR}-stamp"
   )
+add_custom_target(dinfo DEPENDS 
"${CMAKE_CURRENT_BINARY_DIR}/test-${CMAKE_CFG_INTDIR}-stamp")
 
+# Testing thus far indicates that an install rule may point to a file generated
+# by a command such as the one above, but since it cannot be listed as an
+# OUTPUT from the build system's perspective due to issue 12877 the generation
+# of the file is a side effect of which the build system has no knowledge.
+# Consequently, an install rule referencing such a file will not trigger the
+# custom target and the install will fail unless the custom target is first
+# independently built.
+#
+# However, since the build system doesn't appear to validate that the file it
+# is trying to install is either a) present at configure time or b) a build
+# output we may still define an install rule for such a generated file - the
+# problem is we have no way to tell the "make install step to trigger the
+# script execution if it is missing the file.
+
+# To avoid this we can define a custom target that specifies ALL in its
+# properties that depends on the target responsible for generating the targeted
+# install file.  The ALL target will be built before install and will thus
+# trigger the script execution needed for output generation.  (This is a
+# work-around rather than a proper solution with target level awareness, but
+# appears to be the best we can currently do.  However, the same ALL target
+# should be usable for all custom targets for which we need such a workaround.)
+add_custom_target(do_custom ALL DEPENDS dinfo)
+install(FILES "$<TARGET_FILE_DIR:dir_info>/${RBIN_DIR}/doc/test_file.txt" 
DESTINATION doc)
+
 # Local Variables:
 # tab-width: 8
 # mode: cmake

Modified: brlcad/trunk/doc/notes/cmake_paths/test.cmake.in
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/test.cmake.in    2020-06-01 16:07:05 UTC 
(rev 75995)
+++ brlcad/trunk/doc/notes/cmake_paths/test.cmake.in    2020-06-01 17:04:16 UTC 
(rev 75996)
@@ -14,6 +14,8 @@
 string(REGEX REPLACE "${BIN_DIR}$" "" ROOT_DIR "${EXEC_DIR}")
 message(ROOT_DIR: ${ROOT_DIR})
 
+file(WRITE ${ROOT_DIR}/doc/test_file.txt "test file")
+
 # Local Variables:
 # tab-width: 8
 # mode: cmake

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to