modules icon indicating copy to clipboard operation
modules copied to clipboard

better code to pass all arguments of cmake function to `execute_process`

Open xdelaruelle opened this issue 3 years ago • 2 comments

Current CMake code that defines the module command could be improved to handle better the arguments that the function receives. In current code there is a test on the number of arguments to call the execute_process CMake command with the ARGVX variables specifically set:

function(module)
   cmake_policy(SET CMP0007 NEW)
   set(_mlstatus TRUE)
   execute_process(COMMAND mktemp -t moduleinit.cmake.XXXXXXXXXXXX
      OUTPUT_VARIABLE tempfile_name
      OUTPUT_STRIP_TRAILING_WHITESPACE)
   if(${ARGC} EQUAL 1)
      execute_process(COMMAND /usr/bin/tclsh /path/to/modules/modulecmd.tcl cmake "${ARGV0}"
         OUTPUT_FILE ${tempfile_name})
   elseif(${ARGC} EQUAL 2)
      execute_process(COMMAND /usr/bin/tclsh /path/to/modules/modulecmd.tcl cmake "${ARGV0}" "${ARGV1}"
         OUTPUT_FILE ${tempfile_name})
   elseif(${ARGC} EQUAL 3)
      execute_process(COMMAND /usr/bin/tclsh /path/to/modules/modulecmd.tcl cmake "${ARGV0}" "${ARGV1}" "${ARGV2}"
         OUTPUT_FILE ${tempfile_name})
   elseif(${ARGC} EQUAL 4)
      execute_process(COMMAND /usr/bin/tclsh /path/to/modules/modulecmd.tcl cmake "${ARGV0}" "${ARGV1}" "${ARGV2}" "${ARGV3}"
         OUTPUT_FILE ${tempfile_name})
   else()
      execute_process(COMMAND /usr/bin/tclsh /path/to/modules/modulecmd.tcl cmake ${ARGV}
         OUTPUT_FILE ${tempfile_name})
   endif()
   if(EXISTS ${tempfile_name})
      include(${tempfile_name})
      file(REMOVE ${tempfile_name})
   endif()
   set(module_result ${_mlstatus} PARENT_SCOPE)
endfunction(module)

Someone with better CMake skills may find a simpler code to call execute_process with the arguments passed to the module function.

To change the module function definition, update the renderAutoinit procedure in the tcl/envmngt.tcl.in file.

To check the produced module function definition:

$ make modulecmd.tcl
$ ./modulecmd.tcl cmake autoinit

Then to test if this code is performing correctly:

$ make testinstall

xdelaruelle avatar Sep 26 '21 18:09 xdelaruelle

At present, the number of arguments is hardcoded and checked using if condition. So the function has to be modified such that it should be able execute variable number of commands provided via arguments?

nkilm avatar Oct 08 '22 05:10 nkilm

@nkilm Function has to be modified such that it should be able to execute a command with a variable number of arguments provided

xdelaruelle avatar Oct 09 '22 18:10 xdelaruelle