2

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

  • What's the problem with the solutions from the other posts? – Silvano Cerza Jul 15 '19 at 13:35
  • @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é Jul 15 '19 at 13:44
  • How is exactly structured the project? In which folders are the `pro` files? Are there any `pri`? Can you show us these files? – Silvano Cerza Jul 15 '19 at 13:49
  • I will edit the question and put the detailed structed – Mathieu Minié Jul 15 '19 at 14:00
  • Does `testApp.pro` includes `api.pro`? How does it depend on it? – Silvano Cerza Jul 15 '19 at 14:16
  • 1
    @SilvanoCerza The dependency is configured in testProject.pro I'll update the question to clarify it. – Mathieu Minié Jul 15 '19 at 14:21
  • That's strange, with a project structured like yours my library is rebuilt every time. Maybe the `pro` files are different? Could you post those too? – Silvano Cerza Jul 15 '19 at 14:41
  • 1
    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é Jul 15 '19 at 14:59
  • So you build `API` one time, copy it over the build dir and then only build `testApp`? But you want to run the shell script each time `testApp` is built? Or only when `API` is built? Would it be ok to execute the script each time `qmake` runs? – Silvano Cerza Jul 15 '19 at 15:16
  • Yes I compile the API to generate a dynamic library which I automatically copy in the testApp folder for it to be able to use the new library. However I only want the script to be run when the API is recompiled (because I want to use the script to generate data concerning the new API),meaning when there has been a change in the API code. So running the script when qmake does will actually run the script even if the API hasn't been modified right ? – Mathieu Minié Jul 15 '19 at 15:25
  • Yes, even if it's not modified. Anyway if the script target is inside `api.pro` it must run when building it, there must be something else in it, I'd try to go by exclusion and gradually remove small parts to see what happens. – Silvano Cerza Jul 15 '19 at 15:37

2 Answers2

0

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
Rick Pat
  • 789
  • 5
  • 14
  • 2
    Unfortunately, I am building a library which needs to be non qt-configuration dependant and that every person who clones it can run without changing build step. Ideally (if it is really possible) I would like to make the .pro file do everything automaically – Mathieu Minié Jul 15 '19 at 13:41
0

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
Matt
  • 13,674
  • 1
  • 18
  • 27
  • Unfortunately this option doesn't work either ... In fact while my attempt ran the script on rebuild, this one doesn't. In my attempts I have noticed that not using ```system``` won't work and I also added `#! /bin/bash` at the beginning of my script so I wouldn't need to specify it in the .pro – Mathieu Minié Jul 15 '19 at 15:58
  • I use one project to create a dynamic library and another one which uses this library and creates an executable file (depending on the OS), so yes there is a linking step (though I didn't configured it as I don't even know how to do so). – Mathieu Minié Jul 15 '19 at 16:09
  • @MathieuMinié Just look into the generated makefile and find that rule. The line invoking shell script must be there just after linker/librarian call. If not then you did something wrong. Regenerate makefile etc. – Matt Jul 15 '19 at 16:12
  • Indeed the line invoking my script is in the makefile of the API project, though my script isn't running when it is supposed to, what is that supposed to mean ? – Mathieu Minié Jul 15 '19 at 21:55
  • @MathieuMinié This is why here on SO we demand a "minimal complete verifiable example". Otherwise, we end up guessing. Anyway, it's not about qmake, it's a shell issue. Modes/permissions/typos/bashisms etc. Read some good book on a subject. – Matt Jul 16 '19 at 08:52
  • I added a blank project on GITHUB with the script and my call to the script integrated in the .pro file , the link to clone is in the question. – Mathieu Minié Jul 16 '19 at 12:18