Skip to content

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

Closed

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

  1. nkilm commented on Oct 8, 2022

    @nkilm

    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?

  2. xdelaruelle commented on Oct 9, 2022

    @xdelaruelle
    CollaboratorAuthor

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

  3. added this to the 5.7.0 milestone on Oct 27, 2025
  4. added a commit that references this issue on Dec 5, 2025
    eac837d
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