Conan: Allow to pass CMAKE_TOOLCHAIN_FILE variable to cmake during cross-compilation

Created on 28 Jun 2017  路  11Comments  路  Source: conan-io/conan

I'm cross-compiling several packages for Orbis platform (PlayStation4) using CMake. I have cmake toolchain file that defines several variables (mainly CMAKE_GENERATOR_PLATFORMand CMAKE_GENERATOR_TOOLSET) to handle the process. I would like to use it while producing conan packages without explicitly defining it in each conanfile.py. I had seen the thread #160 and tried to use method described in http://docs.conan.io/en/latest/howtos/cross_building.html (creating a profile), but the CONAN_CMAKE_* environment variables are not enough to model the solution.
I would need a variable like CONAN_CMAKE_TOOLCHAIN_FILE or possibility to pass arbitrary flag to cmake (like CONAN_ENV_XXXX_YYYY). Could you add it? Or maybe it's possible to model it in different way?

Most helpful comment

Released in 0.25

All 11 comments

Hi @rbierbasz-gog

I think that the best way to model and use a toolchain would be using environment variables, I have used them for this as follows:

  • I have my toolchain files somewhere in my system
  • I define a profile, with defined settings (in your case, I guess that you have customized your settings.yml to add Orbis), and defining an environment variable CONAN_CMAKE_TOOLCHAIN_FILE to point to the file with the toolchain
  • In my recipes, I add to my cmake command something like:
toolchain = os.getenv("CONAN_CMAKE_TOOLCHAIN_FILE ", None)
toolchain = ("-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain) if toolchain else ""
self.run("cmake %s %s" % (cmake.command_line, toolchain))

Please note that the above is a general approach: as you can define environment variables per package, you are allowed to define different toolchains per package, which sometimes can be necessary.

Could that be automatically managed by the cmake helper? Lets consider it, thanks for the suggestion. What do you think, @lasote?

Please tell me if the above makes sense. Thanks!

Hello @memsharded

thanks for the quick response. Of course what you presented made sense, but - as I had mentioned before - the most convenient way of achieving it would be without modifying conanfile of each package. You already allow to pass several variables to cmake, and passing CMAKE_TOOLCHAIN_FILE will allow to set any variable. Even the cmake wiki (http://www.vtk.org/Wiki/CMake_Cross_Compiling) that you link on the cross-building doc page (http://docs.conan.io/en/latest/howtos/cross_building.html) recommends using toolchain file :

Defining all the variables mentioned above using -DCMAKE_SYSTEM_NAME etc. would be quite tedious and error prone. To make things easier, there is another cmake variable you can set:
CMAKE_TOOLCHAIN_FILE
absolute or relative path to a cmake script which sets up all the toolchain related variables mentioned above

Defining all the variables mentioned above using -DCMAKE_SYSTEM_NAME etc. would be quite tedious and error prone.

I agree, but setting the corresponding environment variables in a profile file is almost the same as creating a CMAKE_TOOLCHAIN_FILE. right?

Anyway I don't have anything against it. We could add a CONAN_CMAKE_TOOLCHAIN_FILE variable handling. Do you think it should have priority against the rest of manual variables?

["CONAN_CMAKE_SYSTEM_PROCESSOR",
 "CONAN_CMAKE_FIND_ROOT_PATH",
 "CONAN_CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
 "CONAN_CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
 "CONAN_CMAKE_FIND_ROOT_PATH_MODE_INCLUDE"]

About adding custom CMake variables through the environment, I would like to have a more solid use case before doing it. And definitely would have to be distinguished from others, for example with a prefix CONAN_CMAKE_CUSTOM_XXXX

I agree, but setting the corresponding environment variables in a profile file is almost the same as creating a CMAKE_TOOLCHAIN_FILE. right?

Not sure what you mean. CMake doesn't read environment variables, you have to pass/define them in the command line.

So the proposal would be, making CMake helper, read the CONAN_CMAKE_TOOLCHAIN_FILE environment variable, and if defined, automatically setting in the command line -DCMAKE_TOOLCHAIN_FILE=<value>, right?

My point is the CMAKE_TOOLCHAIN_FILE definition was invented to not have multiple CMake definition scattered, right? But with conan profiles, you can have a similar thing to a CMAKE_TOOLCHAIN_FILE with a profile declaring the variables mentioned in my last comment.

But again, I'm ok introducing the CONAN_CMAKE_TOOLCHAIN_FILE environment variable

Ok, approved (for 0.26, sorry, 0.25 is already packed). The only thing to be tested and defined is the priority wrt the individual variables.

Yep, let's see what does CMake if you pass the toolchain and a variable and let's do the same.

My point is the CMAKE_TOOLCHAIN_FILE definition was invented to not have multiple CMake definition scattered, right? But with conan profiles, you can have a similar thing to a CMAKE_TOOLCHAIN_FILE with a profile declaring the variables mentioned in my last comment.

For simple toolchain files it's correct, but there are toolchains with more than just set operations - take a look at https://github.com/cristeab/ios-cmake/blob/master/toolchain/iOS.cmake - it uses exec_program, find_program, etc.

Of course, I agree.

Released in 0.25

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uilianries picture uilianries  路  3Comments

niosHD picture niosHD  路  3Comments

Polibif picture Polibif  路  3Comments

mpdelbuono picture mpdelbuono  路  3Comments

theodelrieu picture theodelrieu  路  3Comments