1

I made an installer for my software using cmake and cpack. in the cpack installer the user chose where to install the software. let's say C:\Users\MySoftware

all my .exe and a python file go in it using the Destination option of cmake. using cpack, it adds C:\Users\MySoftware to my environment variable. then all my .exe are usable from the window cmd.

I have a .bat which run the multiple .exe in some order with some options etc ... and in the middle of it, it runs a python script.

python C:\HardPATH/mypython.py -i %var1% -m %var2% -t %var3% -o %var4% -I %var5% . 

I am trying to modify C:\HardPath when using cmake or cpack

My problem is that I don't find a way to edit "HardPATH" using cmake or cpack, according to where the user choose to install the software. and because it's a .py and not .exe, even if it's in the folder added to the env path it doesn't work. I tried using configure_file but it's executed to soon, at configure step, so before the user choose the "HardPATH" using cpack installer. maybe something is possible using add_custom_command ? but I don't get how to change the hardPATH in the .bat using this command, or if it's possible.

Any idea how i could solve this "HardPATH" problem ?

  IF(CMAKE_SYSTEM_NAME STREQUAL Windows)
    IF(Pack)
     INSTALL(TARGETS 
            exe1
            exe2
            etc
            DESTINATION bin)

     find_program(PYTHON "python")

    if (PYTHON)
     INSTALL(FILES mypython.py DESTINATION bin)
    endif()

   ELSE(Pack)
    INSTALL(TARGETS 
            exe1
            exe2
            etc
            DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

    find_program(PYTHON "python")
    if (PYTHON)
      INSTALL(FILES mypython.py DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
    endif()
  ENDIF(Pack)

  ENDIF(CMAKE_SYSTEM_NAME STREQUAL Windows)

and to add the path in the env variable, in the main cmakelist calling this one, I have:

set(CPACK_PACKAGE_NAME "CPackExampleInstaller")
SET(CPACK_NSIS_MODIFY_PATH ON)
INCLUDE(CPack)

if I type echo %PATH% I see where I chose to install mysoftware in it.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9. 0\libnvvp;C:\Python36\Scripts\;C:\Python36\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Pro gram Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\CMake\bin;C:\Program Files\Git\cmd;C:\Program Files\Git\m ingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\xxx\Desktop\testCode\bin;;C:\Users\xxxx\AppData\Local\GitHubDesktop\bin;D:\xxxx\Docker Toolbox

and i can call the .exe within C:\Users\xxx\Desktop\testCode\bin without typing the full path. but not the python

p.deman
  • 584
  • 2
  • 10
  • 24
  • How about you post the code of the .bat file, then perhaps I can be clear on what you want. – Gerhard Feb 08 '18 at 13:37
  • sorry I thought it was clear enough. python C:\HardPATH/mypython.py -i %var1% -m %var2% -t %var3% -o %var4% -I %var5% . I am trying to modify C:\HardPath when using cmake or cpack – p.deman Feb 08 '18 at 13:41
  • Then send the path as a variable and catch it using `%1` I cannot show you though because I do not have any of your code. Also, do not add code to comments, instead edit your question and add it there. – Gerhard Feb 08 '18 at 13:43
  • I don't get what you mean. which code do you want ? the cmakelists.txt ? the .bat file ? I don't understand – p.deman Feb 08 '18 at 13:45
  • Yes, you want help with your code, so post some code. – Gerhard Feb 08 '18 at 13:46
  • yes but which one ? the cmakelist ? – p.deman Feb 08 '18 at 13:47
  • Yes, please. I need to see how your processes fit together. – Gerhard Feb 08 '18 at 13:48
  • So you actually store the Hardpath as an environment variable? – Gerhard Feb 08 '18 at 13:59
  • yes it is an env variable. if I do echo %PATH%, I have plenty, including HardPATH, but I don't know how to find it from the many in %PATH% – p.deman Feb 08 '18 at 14:00
  • but if you add it to the path you do not have to call hardpath anymore..... You then just do `python mypython.py -I....` because mypython.py is in the path – Gerhard Feb 08 '18 at 14:03
  • no I tried it's not working. I was expecting it would work like this but it's not. it says the file doesn't exist. it's working for the .exe but not the .py – p.deman Feb 08 '18 at 14:05
  • Do you call the batch file after the make? If not, you need to recall the batch to read from the newly set env – Gerhard Feb 08 '18 at 14:06
  • yes I call it after the make. I opened a new cmd prompt after the make as well. (I haven't restart the computer. but I thought opening a new cmd should be enough) – p.deman Feb 08 '18 at 14:16
  • Does not seem right. Can you do echo %PATH% and copy the output to the question? – Gerhard Feb 08 '18 at 14:17
  • Just so you know, there are two ;; after the relevant path. `C:\Users\xxx\Desktop\testCode\bin;;` – Gerhard Feb 08 '18 at 19:25
  • yes I noticed, but it's added using CPack, I just give C:\Users\xxx\Desktop\testCode\bin to it, I have no idea why there is two ;; and don't think I have a control on it. – p.deman Feb 09 '18 at 13:51

1 Answers1

1

Use configure_file.

Instead of the final .bat with the hardcoded path, your source tree only contains a template for the final file:

rem File: foo.bat.in
python @MY_PATH@/mypython.py -i %var1% -m %var2% -t %var3% -o %var4% -I %var5% . 

You then use CMake to generate the real batch file into the binary tree:

set(MY_PATH ${CMAKE_INSTALL_PREFIX}/python)
configure_file(${PROJECT_SOURCE_DIR}/foo.bat.in ${PROJECT_BINARY_DIR}/foo.bat @ONLY)

Then you install the .bat file from the binary tree.

Note that this is still a bad idea. The convention is that files in the install tree are relocatable, so having hardcoded absolute paths anywhere in your install artifacts is not a nice thing to do and will likely upset your users. Better change your scripts so that they work with relative paths only.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • thanks. this is what I was looking for. I am going to try soon (don't have access my computer for now). I am just afraid about the non relative path, I had some problem with cpack and this but I am going to try soon – p.deman Feb 09 '18 at 14:00
  • so I have done this: set(MY_PATH $CPACK_PACKAGE_DIRECTORY/bin) configure_file(Test.bat.in $CPACK_PACKAGE_DIRECTORY/bin/Test.bat @ONLY) but it doesn't work. the .bat does not appear in the directory selected by the user using the cpack interface – p.deman Feb 12 '18 at 10:10
  • You need two steps, as described above: You configure to the build tree and install to the install tree. Do not attempt to cut corners by mixing the two. – ComicSansMS Feb 12 '18 at 10:29
  • I don't understand what you mean. in my source, I have the .bat.in in which I set python @MY_PATH@/mypython.py etc ... I want MY_PATH to be the path specified by the user using cpack install. this variable is $CPACK_PACKAGE_DIRECTORY right ? and I want this .bat to go in the directory indicated by the path. so what you are saying is that I should do: set(MY_PATH $CPACK_PACKAGE_DIRECTORY/bin) configure_file(Test.bat.in $CMAKE_BINARY_DIR/Test.bat @ONLY), and then INSTALL(FILES Test.bat DESTINATION bin) ? I am trying without sucess for now – p.deman Feb 12 '18 at 10:42
  • I have python $CPACK_PACKAGE_DIRECTORY/mypython.py in the build directory after performing the cmake configuration and generation. and when I generate the cpack installer and install it doen't move the .bat where I want – p.deman Feb 12 '18 at 10:54
  • is there a way to use configure_file or equivalent "inside" cpack ? because apparently here it modifies the .bat before the user choose where to install – p.deman Feb 13 '18 at 07:27
  • is it possible to do something with add_custom_command which seems to be execute later than configure_file ? at build step. – p.deman Feb 13 '18 at 07:40