samedi 27 juin 2015

How can I use CMake to both build wxwidgets on-demand and link with it

I have the following situation:

  • I'm working on an application that depends on a number of third party libs, among them wxwidgets
  • I build the application for multiple target configurations (x86, arm, Linux, Windows) using Linux as my build host system
  • Due to the above mentioned multiple target configurations, I have chosen to build those third-party libs from source, using CMake's ExternalProject_Add function.
  • The third-party libs are built 'on-demand' at a location separate from my application's CMAKE_BINARY_DIR so that I can wipe the build tree for my application without having to rebuild the third-party libs (takes a looooong time).
  • The location of the third-party libs is different depending on what target configuration I build them for (obviously).

I'm quite new to CMake and the problem I currently face is this: The source files in my application can't find the wx include files and I need to set the correct linker flags to be able to link my application against wxwidgets.

This seems to be handled by a utility 'wx-config' that provides exactly that info as output when run with either the --cppflags or --libs flag. I can not however, figure out how to catch that output and append it to the include dirs and linked libraries I setup from my CMakeLists.txt files.

So basically what I want is.

  1. Build wxwidgets (if it doesn't exist) for the current target configuration
  2. Run wx-config --cppflags and --libs to find out the correct include dirs and linker flags for the current target configuration
  3. Use the info from step 2 when building targets that are my own application

So far I've tried something like this:

# Set a target-configuration-specific location 
set(wxwidgetsTop ${MYPROJECT_EXTERNAL_DIR}/wxwidgets/wxwidgets_${MYPROJECT_CURRENT_TARGET_CFG})

# Build the project
ExternalProject_Add( wxWidgetsExternal
  PREFIX ${wxwidgetsTop}
  URL ${MYPROJECT_EXTERNAL_DIR}/tarballs/wxWidgets-3.0.2.tar.bz2
  SOURCE_DIR ${wxwidgetsTop}/src/wxwidgets
  CONFIGURE_COMMAND ${configure_cmdline}
  BUILD_COMMAND make -j${MYPROJECT_NCPU}
  INSTALL_COMMAND make install
  )

# Create a wxwidgets target to be used as a dependency from other code
add_library(wxWidgets IMPORTED STATIC GLOBAL)
add_dependencies(wxWidgets wxWidgetsExternal)

# (non-working) attempt to get the correct include dirs and linker
# flags for wxwidgets 
add_custom_command(TARGET wxWidgetsExternal
  POST_BUILD
  COMMAND ${INSTALL_DIR}/bin/wx-config ARGS --cppflags
  COMMENT "Running wx-config"
)

but the above does not provide a way to actually use the result from the custom command to append the cppflags and linker options when building the targets that make up my application.

What is a good way to achieve what I want?

Aucun commentaire:

Enregistrer un commentaire