Emscripten: Preferred CMake flag to detect if on Emscripten

Created on 14 Feb 2015  路  7Comments  路  Source: emscripten-core/emscripten

Hi,

I try to build a cross-platform CMakeLists.txt that allows me to configure and build both under Windows with msvc and on Emscripten. Now, the compilation and linking steps all succeed for both compilers/platforms, but I want to get rid of some warnings emited by emcc, using "-Wno-warn-absolute-paths" to not let it complain about absolute paths that CMake always uses when referencing library and include paths and also to activate C++11 support via "-std=c++11" to not let it complain about some range-based for loops I am using.
I wanted to include an add_definitions instruction in my CMake build file wrapped in an if()endif() but so far, I have yet to find the correct condition that tells me that I am under emcc.
I also do not want to identify a GNU or Clang compiler there, because I still want to be able to compile with the real Clang and x86 LLVM backends, and not with Emscripten/fastcomp.

What is the preferred condition here to detect if on emcc/em++?

Thanks!

CMake

Most helpful comment

All 7 comments

I use linux/em++-1.29, linux/clang++-3.5, linux/g++-4.9, windows/msvc++-2015 and etc. with cmake. My CMakeLists.txt has the additional compiler detection code:

if(CMAKE_CXX_COMPILER MATCHES "/em\\+\\+(-[a-zA-Z0-9.])?$")
  message(" * C++ compiler: Emscripten")
  set(CMAKE_CXX_COMPILER_ID "Emscripten")
else()
  message(" * C++ cimpiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

and use the ${CMAKE_CXX_COMPILER_ID} like:

if(CMAKE_CXX_COMPILER_ID STREQUAL "Emscripten")
  ...
elseif("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  ...

And appendix, you can detect the C++11 supporting in CMakeLists.txt like:

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
  message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support.")
endif()

And, you can change cmake variables if you need:

  • CMAKE_LIBRARY_PATH
  • CMAKE_INCLUDE_PATH
  • CMAKE_SYSTEM_LIBRARY_PATH
  • CMAKE_PREFIX_PATH

eg.: set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /aaa /bbb/ccc)
ref.: http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_LIBRARY_PATH.html

Cool, thank you for these great information, very helpful!
I am a bit new to CMake and horribly new to Emscripten, but that will definitely get me started. :)

Hm.. somehow, the compiler detection does not work. When I do:
emconfigure cmake .. -G "MinGW Makefiles"
on my CMakeLists.txt I get the output:

  • C++ cimpiler: Clang
    and not Emscripten. Is there something else I need to do to let it correctly recognize Emscripten compiler?

Ah, figured that one out myself. :) I had to alter the matching regex to this:
"/em\+\+(-[a-zA-Z0-9.])?(\.bat)?$"
as it seemed that the variable ended on .bat on Windows.

Was this page helpful?
0 / 5 - 0 ratings