1

By exemple, I can define CORE_DIR = "<please change it with your relative path to the core component>" in the pro file. Then, the user John could change it in its own customized file (environment) to CORE_DIR = "../../../../john's fancy core". (This customized file would be small and clear enough to not be added to the version control system). So, how can I adapt a project to the user's environment (to have the said behaviour)?

This way I can avoid forcing CORE_DIR value (and so the local paths) for everybody.

Liviu
  • 1,859
  • 2
  • 22
  • 48
  • What about adding `include(custom.pro)` in your _.pro_ file? – BillyJoe Mar 17 '17 at 15:25
  • @mbjoe well, it works better than I expected ... except for the message `"Cannot read D:/Sources/core/qt/custom.pri: No such file or directory"`. The standard sample I just found also talks about custom options: http://doc.qt.io/qt-5/qmake-test-function-reference.html#include-filename – Liviu Mar 17 '17 at 15:52
  • I like the fact QT can work with the file missing because I do not want to add it to the version control. – Liviu Mar 17 '17 at 15:53

1 Answers1

2

I have implemented this in a nice way like this:

# PUT YOUR LOCAL OVERRIDES IN THIS FILE AND DON'T INCLUDE IT IN GIT!
!include( local_overrides.pri ) {
    LOCAL_OVERRIDES=false
}else{
    LOCAL_OVERRIDES=true
}

Later I can check if local_overrides.pri was indeed present and included by inspecting LOCAL_OVERRIDES like so:

message("LOCAL_OVERRIDES ENABLED: " $${LOCAL_OVERRIDES})

Works like a charm. You could also go further and actually create the file with sane defaults if it was missing. I haven't ventured there yet.

EDIT: I decided to put an example for how you could create the file if it does not exist:

!exists( local_overrides.pri ) {
    # Please note the single > to truncate existing file if present (should not be necessary but you never know)
    system("echo 'MY_VAR1=sane_default' > local_overrides.pri")
    # Please note the double >> to append instead of truncate
    system("echo 'MY_VAR2=other_sane_default' >> local_overrides.pri")
}

!include( local_overrides.pri ) {
    error(ERROR: Could not load local_overrides.pri. Aborting build)
}

NOTE: This is an untested example

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • "You could also go further and actually create the file with sane defaults" - without adding it to git? – Liviu Mar 21 '17 at 08:43
  • Yes, just use a bunch of system("echo 'whatever' >> local_overrides.pri") or similar. Documentation for system() is here: http://doc.qt.io/qt-5/qmake-test-function-reference.html#system-command – Mr. Developerdude Mar 21 '17 at 09:52
  • I added an example to the answer. NOTE: It's untested and might not work! – Mr. Developerdude Mar 21 '17 at 10:00
  • Thank you, I'll think about it, maybe writing all the variables in a file goes a little bit too far (when there are many variables) and there is need to change only one or two. Plus, now people are forced to maintain it. – Liviu Mar 21 '17 at 10:52
  • Anyway, I like the answer, the fact that it is complete. Thank you again – Liviu Mar 21 '17 at 10:53
  • I agree completely. I would not want to automate this. In my projects I simply document what kind of variables are expected in local_overrides.pri in a comment in the main.pro file (since ANY qmake syntax is possible ). Finally, you could replace the system() calls with touch() instead to simply make the empty file. Documentation here: http://doc.qt.io/qt-5/qmake-test-function-reference.html#touch-filename-reference-filename – Mr. Developerdude Mar 21 '17 at 12:13
  • `touch` needs a second parameter, but even then ... it does not work on Windows: http://stackoverflow.com/questions/36181618/qmake-touch-function-on-windows. On the other hand, `system("echo. > ../../local_overrides.pri")` works like 'touch', I believe. – Liviu Mar 27 '17 at 14:15
  • It works for me without worries so probably I will accept this solution in April. Thank you! – Liviu Mar 28 '17 at 07:37