[SOLVED] Having custom variables in Makefile



  • Hello,

    I have a .pro file. That needs a path. For example:

    Project file

    ROOT = dummyPath
    MYPATH = $(ROOT)/myappPath
    

    $(ROOT) will be coming from my Makefile.

    I have a custom Makefile, that runs qmake. Example:

    ROOT=/actualPath/
    all:
            qmake -o Makefilegen testqmake.pro
    

    I want to take ROOT from my Makefile and use it in .pro file and finally while generating the Makefile from .pro file, I would like to have something like this:

    ROOT=/actualPath/
    MYPATH = /actualPath//myappPath
    

    How to do this?

    I know I am trying to solve my own problem here.
    But the basic questions are

    • How to pass a variable to qmake
    • How to make that variable to be part of generated Makefile.

    Please let me know.

    Thank you,
    Kumara



  • Haven't tried it, but check out the discussion here.


  • Lifetime Qt Champion

    Hi,

    Do you mean something like:

    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .
    
    # Input
    SOURCES += main.cpp
    DEFINES += MyData=$${TEST}
    message(Environment Variable:  $${TEST})
    

    ?



  • Hi,

    Later to a quick search and experiments, I have got what I have wanted but partially.

    Here are the details:

    My Makefile looks like this:

    export BOOST_LIBS_PATH=/path/to/boost
    
    all:
            qmake -o Makefilegen testqmake.pro
    

    As it says, it exports a path, and creates Makefilegen using testqmake.pro.

    testqmake.pro looks like this:

    TEMPLATE = app
    TARGET = myapp
    DEPENDPATH += .
    INCLUDEPATH += .
    
    # Input
    SOURCES += main.cpp
    
    DEFINES += "MYPATH=\"$(BOOST_LIBS_PATH)\""
    
    message("Master pro file path : ["$(BOOST_LIBS_PATH)"]")
    
    MYTESTPATH = $(BOOST_LIBS_PATH)/er/er/er/e
    
    message("My path : ["$${MYTESTPATH}"]")
    

    And in the console

    Project MESSAGE: Master pro file path : [/path/to/boost]
    Project MESSAGE: My path : [/path/to/boost/er/er/er/e]
    Project MESSAGE: My path : [/lib/aaa/bbb]
    

    But, I want to take this to my source code. But it doesn't work

    DEFINES += "MYPATH=\"$(BOOST_LIBS_PATH)\""
    

    In the generated Makefile

    DEFINES       = -DQT_WEBKIT -DMYPATH=$(BOOST_LIBS_PATH) -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
    

    I wish to get as

    DEFINES       = -DQT_WEBKIT -DMYPATH="/path/to/boost" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
    

    How do I pass a variable to defines?


  • Lifetime Qt Champion

    You'll have to escape your string more than that:

    TESTSTR = '\"$$(TEST)\"'
    DEFINES += TEST=\"$${TESTSTR}\"

    SOURCES += main.cpp

    [edit: fixed code sample SGaist]



  • @SGaist Thanks Sam.

    With your thoughts,

    WAT+="\"/lib/aaa/bbb"\"
    DEFINES += WAT=$${WAT}
    
    GCPU_PATH=$(BOOST_LIBS_PATH)
    DEFINES+=GCPU_PATH=$${GCPU_PATH}
    

    And this is how the generated Makefile looks like:

    DEFINES       = -DQT_WEBKIT -DWAT="/lib/aaa/bbb" -DGCPU_PATH=$(BOOST_LIBS_PATH) -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
    

    What I see here is, with the local variables, it looks to be working.

    but the environment variable, I am not able to acheive.
    -DGCPU_PATH=$(BOOST_LIBS_PATH) I expect this to be -DGCPU_PATH="/path/to/boost"

    Any more thoughts?

    Thank you,
    Kumara


  • Lifetime Qt Champion

    Sorry, it seems that the code highlighter is not working exactly the same when you are editing the answer and once it's re-loaded.

    I've fixed it. Please take a look again, it's already working for environment variables.



  • Sam, I am bit confused.

    If I understand correctly,

    Having your code,

    TESTSTR = '\\"$\\"'
    DEFINES += TEST=\"$\"
    

    Should I need to add my environment variable here?
    Example:

    TSTR = '\\"$(BOOST_PATH)\\"'
    DEFINES += TEST=$${TSTR}
    

    Makefile looks like this:

    -DTEST=\"$(BOOST_PATH)\"
    

    Or I understood wrongly?

    Please clarify. Thank you very much for your kind help..

    --Kumara


  • Lifetime Qt Champion

    Sorry again about the code formatting…

    Trying again here:

    TESTSTR = '\\"$$(TEST)\\"'
    DEFINES += TEST=\"\$\$\{TESTSTR\}\"

    On that last line remove the backslashes before the dollar signs and curly braces...



  • I did try the exact as you said..
    STRT = '\\"$(BOOST_PATH)\\"'
    DEFINES += BOOST_PATH=\"\$\$\{STRT\}\"
    And the output is,
    -DBOOST_PATH="$${STRT}"

    And I tried removing the back slashes on dollar sign and curly braces
    STRT = '\\"$(BOOST_PATH)\\"'
    DEFINES += BOOST_PATH=\"$${STRT}\"
    And the output is
    -DBOOST_PATH="\"$(BOOST_PATH)\""

    Still no success..

    --Kumara


  • Lifetime Qt Champion

    What do you get for the various variable if you show a message with their content:

    e.g.

    message($$(BOOST_PATH))
    message(\$\$\{STRT\})
    ?

    Again, remove the backslashes in the second line...



  • While printing a message, it looks good.

    message($(BOOST_PATH))

    Project MESSAGE: /path/to/boost
    

    message($${STRT})

    Project MESSAGE: \"/path/to/boost\"
    

    But why this has not been taken to Makefile? Mysteries..


  • Lifetime Qt Champion

    Then you are likely just missing some escaping for the last call...


  • Lifetime Qt Champion

    Finally found a way to write it:

    BOOST = $ $ ( BOOST_PATH )
    BOOST_STR = ' \ \ " $ $ { BOOST } \ \ " '
    DEFINES += BOOST_PATH= \ " $ $ { BOOST_STR } \ "
    

    Extra spaces to be removed for the sample code



  • Thanks Sam, for helping me to fix the problem.

    I do confirm that, it works.

    (I do not want copy the instruction again here, which contains more spaces :P)

    With the above code, I can see this in my Makefile

    -DBOO_PATH="/path/to/boost"
    

    It has been a great help! Thanks a lot.

    --Kumara



  • Later to that,

    I have figured out a problem in having:

    We need to have \" in the argument:
    -DBOO_PATH="/path/to/boost"
    Problem is that, the macro turned out to be
    #define BOO_PATH /path/to/boost
    which is actually useless. We need to have double quotes to cover them up.

    BOOST =  $ $ ( BOOST_PATH )
    BOOST_STR = ' \ \ " $ $ { BOOST } \ \ " '
    DEFINES += BOOST_PATH=\ " $ $ { BOOST_STR } \"
    

    Again, eliminate the spaces :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.