Revision: 75993
          http://sourceforge.net/p/brlcad/code/75993
Author:   starseeker
Date:     2020-06-01 14:05:53 +0000 (Mon, 01 Jun 2020)
Log Message:
-----------
Add a minimal example showing the combination of configure_file and generator 
expressions being used for runtime identification of the current BRL-CAD root 
hierarchy path in build scripts.  This is a bit tricky, but should be a bit 
more 'standard' than the CURRENT_PATH custom mechanism originally added years 
ago for pre-generator-expressions CMake builds to achieve the same result.  
Regression tests are using this method currently - next step will be to convert 
the DocBook logic to using it.  It's not clear yet if the build-type based 
install path customization can use this approach.  (Some portion of it will 
have to persist regardless, as the once-per-configuration-change logic that 
reworks the install files to update their paths depends on the 
current-configuration-named file not being present on the filesystem to 
trigger.)

Modified Paths:
--------------
    brlcad/trunk/doc/CMakeLists.txt

Added Paths:
-----------
    brlcad/trunk/doc/notes/cmake_paths/
    brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt
    brlcad/trunk/doc/notes/cmake_paths/README.txt
    brlcad/trunk/doc/notes/cmake_paths/dir_info.c
    brlcad/trunk/doc/notes/cmake_paths/test.cmake.in

Modified: brlcad/trunk/doc/CMakeLists.txt
===================================================================
--- brlcad/trunk/doc/CMakeLists.txt     2020-06-01 13:45:50 UTC (rev 75992)
+++ brlcad/trunk/doc/CMakeLists.txt     2020-06-01 14:05:53 UTC (rev 75993)
@@ -212,6 +212,10 @@
   STARTERS
   STRATEGY
   notes/bsd_semaphore_bug.txt
+  notes/cmake_paths/CMakeLists.txt
+  notes/cmake_paths/README.txt
+  notes/cmake_paths/dir_info.c
+  notes/cmake_paths/test.cmake.in
   notes/cmake_vars.txt
   notes/csv_to_comgeom.txt
   notes/implicit_constraints.txt

Added: brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt                           
(rev 0)
+++ brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt   2020-06-01 14:05:53 UTC 
(rev 75993)
@@ -0,0 +1,43 @@
+project(CVTEST)
+cmake_minimum_required(VERSION 3.17)
+
+# Set a non-standard, custom binary output path
+set(BIN_DIR deep/dir/bin)
+
+# Use the custom binary output directory to set the standard
+# CMake variables controlling binary output locations
+if(NOT CMAKE_CONFIGURATION_TYPES)
+  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CVTEST_BINARY_DIR}/${BIN_DIR})
+else(NOT CMAKE_CONFIGURATION_TYPES)
+  foreach(CFG_TYPE ${CMAKE_CONFIGURATION_TYPES})
+    if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
+      set(CFG_ROOT ${CVTEST_BINARY_DIR}/${CFG_TYPE})
+    else(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
+      set(CFG_ROOT ${CVTEST_BINARY_DIR})
+    endif(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
+    string(TOUPPER "${CFG_TYPE}" CFG_TYPE_UPPER)
+    set("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CFG_TYPE_UPPER}" 
${CFG_ROOT}/${BIN_DIR})
+  endforeach(CFG_TYPE ${CMAKE_CONFIGURATION_TYPES})
+endif(NOT CMAKE_CONFIGURATION_TYPES)
+
+# Define a target to provide generator properties
+add_executable(dir_info dir_info.c)
+
+# Use configure_file to set up a script and define variables that will
+# not change at runtime.
+configure_file(test.cmake.in test.cmake @ONLY)
+
+# Use the generator expression from dir_info to pass the runtime binary
+# 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
+  COMMAND ${CMAKE_COMMAND} -DEXEC_DIR=$<TARGET_FILE_DIR:dir_info> -P test.cmake
+  )
+
+# Local Variables:
+# tab-width: 8
+# mode: cmake
+# indent-tabs-mode: t
+# End:
+# ex: shiftwidth=2 tabstop=8


Property changes on: brlcad/trunk/doc/notes/cmake_paths/CMakeLists.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/doc/notes/cmake_paths/README.txt
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/README.txt                               
(rev 0)
+++ brlcad/trunk/doc/notes/cmake_paths/README.txt       2020-06-01 14:05:53 UTC 
(rev 75993)
@@ -0,0 +1,59 @@
+This is a minimal example of combining generator expressions, configure_file
+and cmake -P to execute a script at runtime with a multiconfig generator and a
+non-standard BIN_DIR.
+
+Multiconfig build tools change their "root" build output directory at runtime
+to match the current configuration, which means scripts cannot assume a fixed
+path to the build output files.  Historically BRL-CAD handled this by writing
+specific files into the CMake temporary directories in the build dir from a
+custom target in the parent build which was then depended upon by all other
+targets, and having scripts check the current state of that file.  However,
+with the introduction of generator expressions, another mechanism is possible -
+passing an executable build target path to the executing script via a -D option
+and having the script "unpack" that path to deduce the paths.  Unlike BRL-CAD's
+custom solution, this uses standard CMake functionality (given a sufficiently
+new CMake.)
+
+However, to do this robustly a couple of things are needed.  One is an
+executable or library target - custom targets do not provide generator
+expression output.  This is easily handled by making a "dummy" executable whose
+sole purpose is to provide the CMake information from an add_executable target
+(the drawback is that it introduces the requirement for a C compiler, but all
+projects BRL-CAD is likely to be using CMake for should have such a compiler
+present - and in principle any target language will work as long as its build
+target defines the TARGET_FILE* properties.)
+
+Another wrinkle is that the notion of a "bin" directory and "root" directory
+are BRL-CAD conventions, and thus not backed into the CMake target properties.
+To be able to deduce a "root" path, it is necessary to pass the BIN_DIR
+variable into the script to allow the script to "strip down" the generator
+supplied executable path to the root path. (In this example configure_file
+handles it, since the BIN_DIR subpath does not change in the build tool at
+runtime - it's generally easier to read a custom target/command with a smaller
+number of variables passed as -D options.)
+
+Both single and multiconfig behaviors can be tested using the Ninja build
+tool (with recent CMake and Ninja):
+
+user@ubuntu2019:~/cmake_paths/build$ cmake -G Ninja .. && ninja dinfo
+-- Configuring done
+-- Generating done
+-- Build files have been written to: /home/user/cmake_paths/build
+[3/3] cd /home/user/cmake_paths/build && /home/user/cmake-install/... 
-DEXEC_DIR=/home/user/cmake_paths/build/deep/dir/bin -P test.cmake
+EXEC_DIR:/home/user/cmake_paths/build/deep/dir/bin
+BIN_DIR:deep/dir/bin
+ROOT_DIR:/home/user/cmake_paths/build/
+
+user@ubuntu2019:~/cmake_paths/build$ cmake -G "Ninja Multi-Config" .. && ninja 
-f build-Debug.ninja dinfo && ninja -f build-Release.ninja dinfo
+-- Configuring done
+-- Generating done
+-- Build files have been written to: /home/user/cmake_paths/build
+[1/1] cd /home/user/cmake_paths/build && 
/home/user/cmake-install/...C_DIR=/home/user/cmake_paths/build/Debug/deep/dir/bin
 -P test.cmake
+EXEC_DIR:/home/user/cmake_paths/build/Debug/deep/dir/bin
+BIN_DIR:deep/dir/bin
+ROOT_DIR:/home/user/cmake_paths/build/Debug/
+[1/1] cd /home/user/cmake_paths/build && 
/home/user/cmake-install/...DIR=/home/user/cmake_paths/build/Release/deep/dir/bin
 -P test.cmake
+EXEC_DIR:/home/user/cmake_paths/build/Release/deep/dir/bin
+BIN_DIR:deep/dir/bin
+ROOT_DIR:/home/user/cmake_paths/build/Release/
+


Property changes on: brlcad/trunk/doc/notes/cmake_paths/README.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/doc/notes/cmake_paths/dir_info.c
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/dir_info.c                               
(rev 0)
+++ brlcad/trunk/doc/notes/cmake_paths/dir_info.c       2020-06-01 14:05:53 UTC 
(rev 75993)
@@ -0,0 +1,27 @@
+/*                        D I R _ I N F O . C
+ * BRL-CAD
+ *
+ * Published in 2020 by the United States Government.
+ * This work is in the public domain.
+ *
+ */
+/** @file dir_info.c
+ *
+ * This is a stub file, whose purpose is to provide the build logic with an
+ * add_executable build target that can be passed to scripts to supply the
+ * necessary information to identify the runtime location of BR-CAD's root
+ * directory.
+ */
+
+int main() {
+}
+
+/*
+ * Local Variables:
+ * tab-width: 8
+ * mode: C
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: brlcad/trunk/doc/notes/cmake_paths/dir_info.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/doc/notes/cmake_paths/test.cmake.in
===================================================================
--- brlcad/trunk/doc/notes/cmake_paths/test.cmake.in                            
(rev 0)
+++ brlcad/trunk/doc/notes/cmake_paths/test.cmake.in    2020-06-01 14:05:53 UTC 
(rev 75993)
@@ -0,0 +1,22 @@
+# Dequote the generator supplied executable directory - this is needed
+# in case of odd characters (such as spaces) in path names
+string(REPLACE "\\" "" EXEC_DIR "${EXEC_DIR}")
+message(EXEC_DIR: ${EXEC_DIR})
+
+# configure_file will set BIN_DIR from the parent build, so the script
+# knows what the relative binary path is.
+set(BIN_DIR "@BIN_DIR@")
+message(BIN_DIR:  ${BIN_DIR})
+
+# Using the above two pieces of information, construct the "root" path
+# below which the standard BRL-CAD file hiearchy (binary, library, data,
+# doc, etc.) is built.
+string(REGEX REPLACE "${BIN_DIR}$" "" ROOT_DIR "${EXEC_DIR}")
+message(ROOT_DIR: ${ROOT_DIR})
+
+# Local Variables:
+# tab-width: 8
+# mode: cmake
+# indent-tabs-mode: t
+# End:
+# ex: shiftwidth=2 tabstop=8


Property changes on: brlcad/trunk/doc/notes/cmake_paths/test.cmake.in
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
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