qmake: Get parent path / chop one path level



  • Based on the location of the .pro file, I would like to determine the parent path (so effectively chop the last path level).

    E.g. I have
    c:\sources\projects\dummyProject\code\dummy.pro
    Path to pro-File is:
    c:\sources\projects\dummyProject\code
    How can I generate this?
    c:\sources\projects\dummyProject



  • Hi! See http://doc.qt.io/qt-5/qmake-variable-reference.html.

    • _PRO_FILE_ Contains the path to the project file in use.
    • _PRO_FILE_PWD_ Contains the path to the directory containing the project file in use.


  • @Wieland
    Sure. Now how do I get the parent folder of that for further use? Basically what QDir::cdUp() would do.



  • Sorry, I forgot the important part :-) You can just add /../ to the path to move up

    $$_PRO_FILE_PWD_/../



  • Some functions / external tools seem not to like paths like (to continue my above example):

    c:/sources/projects/dummyProject/code/..

    Is there a way to clean up such a path?



  • Yes, it's easy: clean_path( c:/sources/projects/dummyProject/code/.. )



  • @Wieland

    Unfortunately not available in Qt 4.8



  • :-/ Well, how bout using the system's shell?
    NEWPATH = $$system( somecode )



  • @Wieland

    Thank you. You are coming up with ideas upon ideas. Unfortunately, this one has to run both on Windows and Linux. And if it's not a built-in command, it means every build environment must be adapted to support it.

    I think I'm about ready to give up...


  • Lifetime Qt Champion

    Hi,

    Since it's about calling tools on Windows, you probably need to replace the forward slashes before using the path to run the tool you want.



  • @SGaist

    Has to run both on Windows and Linux...


  • Lifetime Qt Champion

    That's why there are scopes that can be used for this kind of situations



  • I have something that works...under some circumstances.
    It seems to always work when running a scripted compile run (Windows and Linux, vs2010 and gcc), but it doesn't seem to work in Creator when evaluating files of the code model.

    defineReplace(replaceBackslashes) {
       TEMP_DIRNAME = $$1
       FINAL_DIRNAME = $$replace(TEMP_DIRNAME,\\\\,/)
       return($$FINAL_DIRNAME)
    }
    
    defineReplace(cleanupPath) {
       TEMP_DIRNAME = $$files($$1)
       FINAL_DIRNAME = $$replaceBackslashes($$TEMP_DIRNAME)
       return($$FINAL_DIRNAME)
    }
    

    For Creator, both TEMP_DIRNAME and FINAL_DIRNAME in "cleanupPath" are always empty...for some reason. In the scripted compile job, they look just the way they are supposed to.

    Any idea why?


  • Lifetime Qt Champion

    Probably because Qt Creator doesn't pass any parameter.

    How are you calling qmake on the command line ?



  • A typical scripted call to qmake would look something like this:

    qmake.exe CONFIG-=debug CONFIG-=release CONFIG-=debug_and_release CONFIG-=build_all CONFIG+=debug CONFIG+=hmk_build -o tpet.mak ..\..\..\code\tpet.pro
    

  • Lifetime Qt Champion

    Then why not use _PRO_FILE_PWD_ as suggested by @Wieland and your replace function to convert forward slashed to back slashes so you wouldn't be dependent on how you call qmake.



  • @SGaist
    Going in circles....
    Certainly, I use the _PRO_FILE_PWD_ as my starting point, but some tools need a clean path without stuff likepwdFolder/../../xyz/folder in it. The function he suggested for cleaning the path is not available in Qt 4.8.

    This brings me back to the question why my original code does not work in context of Creator's code evaluation.
    According to the docs, $files "Expands the specified wildcard pattern and returns a list of filenames. If recursive is true, this function descends into subdirectories."

    Assuming that a folder is also a file (at least, that is how most file systems I know treat folders internally), the function should evaluate the location of the folder, and return the correct clean path for it. And it does so in the scripted qmake call.

    There is no more information in the docs - whether there are certainly boundary conditions for this function to work, limitations, etc. So I'm really at a loss why this does not work as expected.


  • Lifetime Qt Champion

    Try with:

    defineReplace(cleanPath) {
        win32:1 ~= s|\\\\|/|g
        contains(1, ^/.*):pfx = /
        else:pfx =
        segs = $ $split(1, /)
        out =
        for(seg, segs) {
            equals(seg, ..):out = $ $member(out, 0, -2)
            else:!equals(seg, .):out += $ $seg
        }
        win32:return($$join(out, \\, $ $pfx))
        return($$join(out, /, $ $pfx))
    }
    
    sub_dir = $ $_PRO_FILE_PWD_/../testme
    path_for_cmd = $ $cleanPath($ $sub_dir)
    
    message($ $path_for_cmd)
    

    With additional space between $ removed



  • @SGaist
    Wow, thanks! I'll have to find a calm minute to wrap my mind around this...


Log in to reply
 

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