1
votes

I am developping a library and I am facing problems trying to run a shell script each time the code is recompiled i.e. each time the binary is changed.

I am running Qt Creator 4.9.2(based on QT 5.12.4) on ubuntu with GCC 64bits

I have tried using the .commands and QMAKE_EXTRA_TARGETS to run a custom target combined with POST_TARGETDEPS though it does not work. I also tried using QMAKE_POST_LINK though same problem, the result is not the same as expected. Currently, the script is only executed when I change the .pro file (and it is executed two times instead of one) and at the first compilation, then it remains untouched.

I don't know if this is relevant but my project is made of one library and one test code which I have made dependant using SUBDIRS. The architecture is a Global folder containing a .pro file and two sub-folders (library and test code),the library being compiled before the test app when I compile the global project.

I've already tried the solutions brought in these posts :

How to execute shell command after compile finished from .pro in QT?

QMake: execute script after build

https://www.qtcentre.org/threads/46285-How-to-add-the-auto-quot-build-number-quot-in-Qt-Application

The code I currently use is the following one :

    mytarget.commands = @echo $$system($$PWD/build_number.sh)
    mytarget.depends = FORCE

    QMAKE_EXTRA_TARGETS += mytarget
    POST_TARGETDEPS += mytarget

I expect the script to be run one time and one time only when the library is changed i.e. only when the code has been modified.

EDIT : The detailed structure of the project is the following one :

/TestProject  
 |--- testProject.pro  
 |--- API  //this project creates a dynamic library which is exported in testApp project
      |--- api.pro  //where I am trying to run a script  
      |--- ... (source files of lib)  
 |--- testApp  //uses the library previously generated by the API
      |--- testApp.pro  
      |--- ... (source files of app)

The dependency between API and testApp is configured as followed in testProject.pro :

TEMPLATE = subdirs

  SUBDIRS = \
            Api \ 
            testApp

  Api.subdir  = API
  testApp.subdir  = testApp

  testApp.depends = Api  

GITHUB of a blank project using the script :

https://github.com/MMinie/testSHELL

2
What's the problem with the solutions from the other posts? - SilvanoCerza
@SilvanoCerza As mentioned in the post, the shell script only runs when I build the project and at the first compilation after. However it is supposed to run each time the project is recompiled. - Mathieu Minié
How is exactly structured the project? In which folders are the pro files? Are there any pri? Can you show us these files? - SilvanoCerza
@SilvanoCerza The dependency is configured in testProject.pro I'll update the question to clarify it. - Mathieu Minié
My library is recompiled, and automatically copied in the folder of the testApp project. However it is only the shell script which isn't launched except when rebuilding the project. The only part that uses this shell script is the one in the question, the rest of the api.pro file is simple and isn't related to script manipulation - Mathieu Minié

2 Answers

0
votes

Maybe you can work with adding a custom build step in Qt Creator.
It can be done the following way:

  1. In Qt Creator click Projects on the left side.
  2. Select Build in your Kit
  3. Click on the dropdown Add Build Step and select Custom Build Step
  4. Add sh to Command.
  5. Add the path of your script to Arguments
0
votes

I also tried using QMAKE_POST_LINK though same problem, the result is not the same as expected.

I really don't understand what you could make wrong, but that should just work:

QMAKE_POST_LINK=/bin/sh $$PWD/build_number.sh