0

I'm using cmake to build my project now and I want to migrate to qbs in the future. I have some opensource sub-modules from github, which are currently built with cmake, and are included in my project using cmake's add_subdirectory. I've tried to research but found no alternatives of add_subdirectory in qbs.

I don't think migrating all sub-modules build system from cmake to qbs is a good idea because that means I have to migrate sub of sub-modules or sub of sub of sub-modules as well :)

Any help? Thanks!

thamht4190
  • 11
  • 5

1 Answers1

0

To pull in projects built with a different tool, you'll need some sort of wrapper. For instance, assuming your cmake sub-project is a library, you could write this (untested):

Product {
    type: ["dynamiclibrary"]
    Group {
        files: ["subdir/CMakeLists.txt"]
        fileTags: ["cmake_project"]
    }
    Group {
        files: ["subdir/*"]
        excludedFiles: ["subdir/CMakeLists.txt"]
        fileTags: ["cmake_sources"]
    }
    Rule {
        inputs: ["cmake_project"]
        auxiliaryInputs: ["cmake_sources"]
        Artifact {
            filePath: ... // Whatever cmake produces
            fileTags: ["dynamiclibrary"]
        }
        prepare: {
            var cmd = new Command("cmake", [/*cmake arguments*/]);
            cmd.description = "building cmake project xyz";
            cmd.workingDirectory = product.sourceDirectory + "/subdir";
            return [cmd];
        }
    }
}

You should probably tweak the cmake call so that the generated binaries end up in qbs's build directory. There might be convenience functionality for this sort of thing in the future, if it turns out that there's a sensible abstraction level.

  • I got your idea. I tried to create 2 `Rule`s: first `Rule` is to generate Cmake files & project files (use `cmake ./` cmd), then use theses output to be inputs of 2nd `Rule` (use `cmake --build ./` cmd) to build library. It's strange that the 1st `Rule` generates only CMakeFiles folder and no project files. Anything did I miss? – thamht4190 Mar 01 '18 at 09:09
  • after double checking, I realized that 1st `Rule` generates just a part of all outputs, and generated parts is different from time to time. It seems that `Rule` doesn't finish its command. Do you have any idea about that? – thamht4190 Mar 01 '18 at 10:29
  • When the rule runs its commands, it simply executes the specified command lines. Whatever happens then is the invoked tool's business. You can tell whether the command is run by looking for the provided description. You can double-check what is executed (by qbs) with the "--command-echo-mode command-line" option. Note that a rule can contain more than one command, so one rule might suffice. – Christian Kandeler Mar 01 '18 at 13:32
  • Thanks. I merged two Rules into one. Still the problem is that I'm trying to link static libs from cmake-built thirdparty, however, emake take a little long to run, so `cpp.staticLibraries` config failed before cmake finishes generation. Is there anyway `Rule` can be waited to finish? – thamht4190 Mar 02 '18 at 08:00
  • That's not how that works. If cmake is building a static library, then you give your wrapper product the type "staticlibrary" and add a dependency to this product in the product you want to link against the static lib. The cpp.staticLibraries property is for *external* libraries, whereas this one is considered part of the project. – Christian Kandeler Mar 02 '18 at 12:58
  • Oh, that means I have to migrate my submodules from cmake to qbs and should not call external cmake command, right? – thamht4190 Mar 05 '18 at 02:57
  • No, that's not at all what I said. Just make sure you correctly set the Artifact file path to what cmake produces, give it the correct tag, and things will just work. – Christian Kandeler Mar 05 '18 at 10:06
  • Thanks @chris! I did it. – thamht4190 Mar 06 '18 at 03:46
  • could you plz tell the case where the thirdparty generates both static libraries and .h files. I added more artifacts with "hpp" file tag, and added "hpp" into product's type property as well but it seemed that cl.exe runs before .h files are generated. Any suggestion @chris? – thamht4190 Apr 16 '18 at 11:07
  • It works now. However, do you have alternatives if there are a lot of generated .h files rather than listing all of them in artifactOutputs? – thamht4190 Apr 17 '18 at 03:30
  • Haha, now I know why build failed. Actually it succeeds for the first time, then next time, it removes the file in artifactOutputs (but thirdparty project doesn't build again). It leads to error. – thamht4190 Apr 17 '18 at 03:39