Passing QMake variable to a script
-
Hello everyone,
I have a python script that helps me manage versioning, I pass it some QMake variables from QMake.
For example:
CONFIG(debug, debug|release) { CURRENT_BUILD = DEBUG } else { CURRENT_BUILD = RELEASE } message(CURRENT_BUILD = $${CURRENT_BUILD}) PYTHON_SCRIPT_SUCCESS = $$system(python "$${PWD}\compile_helper_2.py" CURRENT_BUILD_MODE=$${CURRENT_BUILD}) message(CURRENT_BUILD = $${CURRENT_BUILD})When I'm running QMake from QtCreator, the 'General Messages' output says:
Project MESSAGE: CURRENT_BUILD = DEBUG Project MESSAGE: CURRENT_BUILD = DEBUGBut My script gets CURRENT_BUILD_MODE = RELEASE
I tried to pass just some string to see if my script doesn't mess up what it receives, like so:
message(CURRENT_BUILD = $${CURRENT_BUILD}) PYTHON_SCRIPT_SUCCESS = $$system(python "$${PWD}\compile_helper_2.py" CURRENT_BUILD_MODE=JUST_SOME_RANDOM_TEXT message(CURRENT_BUILD = $${CURRENT_BUILD})And my script sees:
CURRENT_BUILD_MODE = JUST_SOME_RANDOM_TEXTSo the data passes correctly.
I have no idea why it prints 'DEBUG' in the 'general messages' tab but in the script it sees 'RELEASE',Anyone has any ideas?
I also pass other variables such as
QT_VERSIONandOUT_PWDwhich seem to be working as expected. When I change the build from 32 to 64 bit, the output dir changes accordingly and my script sees it.This also happens when I try to determine the current build bitness like so:
contains(QT_ARCH, i386) { CURRENT_BITNESS = 32-bit } else { CURRENT_BITNESS = 64-bit }In the 'general messages' tab it says 32-bit, but in the script it sees 64-bit.
-
Ok, I made some headway, but am not really closer to a solution.
The general idea of what I'm making is this. My python script gets QT variables from qMake such as build type, build bitness, qt version etc and creates an
inifile with this information. It also puts other useful info in the ini such as compile date and the latest git commit hash. The script later uses this ini file to generate a header file with macros that I use in the code (Such as writing the compile date and release mode in the 'about' section of the app)The generated '.h' file looks like this:
#ifndef COMPILE_INFO_H #define COMPILE_INFO_H #define APP_NAME "Tester APP" #define APP_VERSION "V1.21" #define APP_COMPILE_DATE "06-June-2022" #define APP_GIT_HASH "1500079" #define QT_VERSION_STR "5.15.2" #define CURRENT_BUILD_MODE "RELEASE" #define CURRENT_BITNESS "64-bit" #endif // COMPILE_INFO_H // THIS FILE IS AUTOMATICALLY GENERATED BY A PYTHON AND SHOULD NOT BE EDITED MANUALLY.And this is how the
inifile looks:#Fields inputed manually [VERSION_INFO] APP_NAME =Tester APP APP_VERSION = V1.21 #name of the header file to generate. APP_HEADER_FILENAME = compile_info.h #Everything else is automatically generated by the script [GENERATED_SETTINGS] APP_GIT_HASH = 1500079 APP_COMPILE_DATE = 06-June-2022 [QMAKE_VARIABLES] QT_VERSION_STR = 5.15.2 QT_INSTALL_LIBEXECS = C:/Qt_NEW_INSTALL/5.15.2/mingw81_32/bin OUT_PWD = C:/builddir//build-TEST_APP-Desktop_Qt_5_15_2_MinGW_32_bit-Debug SOURCE_DIR = C:/WorkDir/Development/Source/TEST_APP INI_FILENAME = app_info.ini COUNT = 4 CURRENT_BUILD_MODE = RELEASE CURRENT_BITNESS = 64-bitI tried to print out the 'CURRENT_BITNESS' value in the script. And I see 32-bit. But the output file still says 64bit. After reading online, I saw that qMake usually makes multiple runs (Executed multiple times). And this is indeed what I saw when I printed out messages in qMake using
message(my message). I saw it repeating multiple times. So I thought maybe qMake runs once with the argument being 32-bit and then again with the argument being 64-bit.To test this I first added a COUNT entry to my ini file. And before each write to the file I incremented the variable. And indeed after each time I run qmake, the COUNT variable is incremented by 3.
I tried to limit the amount of times qMake runs the script. I saw in this answer that doing something like this:
!build_pass:message("This message should appear only once")Will limit the printing to only once. I tried it and it indeed worked with printing messages... So I tried it with the execution of the script:
!build_pass:PYTHON_TARGET_STR = $$system(python "$${PWD}\compile_helper_2.py" QT_VERSION_STR=$${QT_VERSION} QT_INSTALL_LIBEXECS=$$[QT_INSTALL_LIBEXECS] OUT_PWD=$${OUT_PWD} CURRENT_BUILD_MODE=$${CURRENT_BUILD} CURRENT_BITNESS=$${CURRENT_BITNESS} SOURCE_DIR=$${PWD} INI_FILENAME=app_info.ini) !build_pass:message(PYTHON_TARGET_STR = $${PYTHON_TARGET_STR})And indeed the message printed only once. However the counter in my ini file still incremented by 3.
I decided to do something else, in addition to just writing the bitness in the ini file I would keep a history of written bitness, by adding a field to the ini file with the current count. So if the count starts at zero the first time the key CURRENT_BITNESS_1 will be added and then the second time CURRENT_BITNESS_2 etc.
And indeed what I got in the ini file was:
COUNT = 3 CURRENT_BUILD_MODE = RELEASE CURRENT_BITNESS = 64-bit CURRENT_BITNESS_1 = 32-bit CURRENT_BITNESS_2 = 32-bit CURRENT_BITNESS_3 = 64-bitSo the suspicion was correct. The value is indeed overwritten in the final run of qMake and I have no idea how to resolve my issue.
TL;DR
How do you make sure that a command in qMake such as
system(python myscript.py)is executed only once? Can QMAKE_POST_LINK be useful here, as described in this answer? -
Ok, I made some headway, but am not really closer to a solution.
The general idea of what I'm making is this. My python script gets QT variables from qMake such as build type, build bitness, qt version etc and creates an
inifile with this information. It also puts other useful info in the ini such as compile date and the latest git commit hash. The script later uses this ini file to generate a header file with macros that I use in the code (Such as writing the compile date and release mode in the 'about' section of the app)The generated '.h' file looks like this:
#ifndef COMPILE_INFO_H #define COMPILE_INFO_H #define APP_NAME "Tester APP" #define APP_VERSION "V1.21" #define APP_COMPILE_DATE "06-June-2022" #define APP_GIT_HASH "1500079" #define QT_VERSION_STR "5.15.2" #define CURRENT_BUILD_MODE "RELEASE" #define CURRENT_BITNESS "64-bit" #endif // COMPILE_INFO_H // THIS FILE IS AUTOMATICALLY GENERATED BY A PYTHON AND SHOULD NOT BE EDITED MANUALLY.And this is how the
inifile looks:#Fields inputed manually [VERSION_INFO] APP_NAME =Tester APP APP_VERSION = V1.21 #name of the header file to generate. APP_HEADER_FILENAME = compile_info.h #Everything else is automatically generated by the script [GENERATED_SETTINGS] APP_GIT_HASH = 1500079 APP_COMPILE_DATE = 06-June-2022 [QMAKE_VARIABLES] QT_VERSION_STR = 5.15.2 QT_INSTALL_LIBEXECS = C:/Qt_NEW_INSTALL/5.15.2/mingw81_32/bin OUT_PWD = C:/builddir//build-TEST_APP-Desktop_Qt_5_15_2_MinGW_32_bit-Debug SOURCE_DIR = C:/WorkDir/Development/Source/TEST_APP INI_FILENAME = app_info.ini COUNT = 4 CURRENT_BUILD_MODE = RELEASE CURRENT_BITNESS = 64-bitI tried to print out the 'CURRENT_BITNESS' value in the script. And I see 32-bit. But the output file still says 64bit. After reading online, I saw that qMake usually makes multiple runs (Executed multiple times). And this is indeed what I saw when I printed out messages in qMake using
message(my message). I saw it repeating multiple times. So I thought maybe qMake runs once with the argument being 32-bit and then again with the argument being 64-bit.To test this I first added a COUNT entry to my ini file. And before each write to the file I incremented the variable. And indeed after each time I run qmake, the COUNT variable is incremented by 3.
I tried to limit the amount of times qMake runs the script. I saw in this answer that doing something like this:
!build_pass:message("This message should appear only once")Will limit the printing to only once. I tried it and it indeed worked with printing messages... So I tried it with the execution of the script:
!build_pass:PYTHON_TARGET_STR = $$system(python "$${PWD}\compile_helper_2.py" QT_VERSION_STR=$${QT_VERSION} QT_INSTALL_LIBEXECS=$$[QT_INSTALL_LIBEXECS] OUT_PWD=$${OUT_PWD} CURRENT_BUILD_MODE=$${CURRENT_BUILD} CURRENT_BITNESS=$${CURRENT_BITNESS} SOURCE_DIR=$${PWD} INI_FILENAME=app_info.ini) !build_pass:message(PYTHON_TARGET_STR = $${PYTHON_TARGET_STR})And indeed the message printed only once. However the counter in my ini file still incremented by 3.
I decided to do something else, in addition to just writing the bitness in the ini file I would keep a history of written bitness, by adding a field to the ini file with the current count. So if the count starts at zero the first time the key CURRENT_BITNESS_1 will be added and then the second time CURRENT_BITNESS_2 etc.
And indeed what I got in the ini file was:
COUNT = 3 CURRENT_BUILD_MODE = RELEASE CURRENT_BITNESS = 64-bit CURRENT_BITNESS_1 = 32-bit CURRENT_BITNESS_2 = 32-bit CURRENT_BITNESS_3 = 64-bitSo the suspicion was correct. The value is indeed overwritten in the final run of qMake and I have no idea how to resolve my issue.
TL;DR
How do you make sure that a command in qMake such as
system(python myscript.py)is executed only once? Can QMAKE_POST_LINK be useful here, as described in this answer?@Curtwagner1984 said in Passing QMake variable to a script:
How do you make sure that a command in qMake such as system(python myscript.py) is executed only once? Can QMAKE_POST_LINK be useful here, as described in this answer?
Yes, QMAKE_POST_LINK or a custom compiler should do the trick.