Skip to content

better code to pass all arguments of cmake function to execute_process #419

Description

@xdelaruelle

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions