7

I have a very simple CMake script. Unfortunately, the project uses a *.pde file which is plain C++ or C code.

CMake is working with any file ending, but I get a compiler error, because GCC does not know how to handle it. How can I add a global file extension to GCC, so that the *.pde file is compiled as a usual *.cpp file?

The -x c++ foo.pde command is nice if I want to use the console, but for CMake it is (I think) not applicable.

cmake_minimum_required(VERSION 2.8)
project(RPiCopter)

SET( RPiCopter
  absdevice
  containers
  device
  exceptions
  navigation
  frame
  vehicle
  receiver
  scheduler
  tinycopter.pde
)

message( STATUS "Include ArduPilot library directories" )
foreach( DIR ${AP_List} ${AP_List_Linux} ${AP_Headers} )
  include_directories( "../libraries/${DIR}" )
endforeach()
include_directories( zserge-jsmn )

# ***************************************
# Build the firmware
# ***************************************
add_subdirectory ( zserge-jsmn )

#set(CMAKE_CXX_FLAGS "-x c++ *.pde")
ADD_EXECUTABLE ( RPiCopter ${RPiCopter} )
target_link_libraries ( RPiCopter -Wl,--start-group ${AP_List} ${AP_List_Linux} jsmn -Wl,--end-group )
Fraser
  • 74,704
  • 20
  • 238
  • 215
dgrat
  • 2,214
  • 4
  • 24
  • 46

4 Answers4

9

You should be able to use set_source_files_properties along with the LANGUAGE property to mark the file(s) as C++ sources:

set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)

As @steveire pointed out in his own answer, this bug will require something like the following workaround:

set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  add_definitions("-x c++")
endif()
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 2
    The `set_source_file_properties()` call is IMO the right way to do it, but I would replace the global `add_definitions()` call to `set_source_properties(${TheFiles} PROPERTIES COMPILE_FLAGS "-x c++")` to limit the definitions to just those files – jairo Apr 27 '17 at 19:41
  • @jairo I agree. `add_definitions` does not work well in case a multi-lang project is built like `project(casm C ASM_NASM)`. – St.Antario Aug 08 '19 at 10:28
4

Normally you should be able to just extend CMAKE_CXX_SOURCE_FILE_EXTENSIONS. This would help, if you have a lot of files with unknown file extensions.

But this variable is not cached - as e.g. CMAKE_CXX_FLAGS is - so the following code in CMakeCXXCompiler.cmake.in will always overwrite/hide whatever you will set:

set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)

I consider this non-caching being a bug in CMake, but until this is going to be changed I searched for a workaround considering the following:

  • You normally don't want to change files in your CMake's installation
  • It won't have any effect if you change CMAKE_CXX_SOURCE_FILE_EXTENSIONS after project()/enable_language() (as discussed here).

I have successfully tested the following using one of the "hooks"/configuration variables inside CMakeCXXCompiler.cmake.in:

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_SYSROOT_FLAG_CODE "list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS pde)")

project(RPiCopter CXX)

message("CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")

add_executable(RPiCopter tinycopter.pde)
Florian
  • 39,996
  • 9
  • 133
  • 149
3

I decided to use this approach. I just remove the file ending by cmake in the temporary build directory. So GCC is not confused anymore because of the strange Arduino *.pde file extension.

# Exchange the file ending of the Arduino project file
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tinycopter.pde ${CMAKE_CURRENT_BINARY_DIR}/tinycopter.cpp)
dgrat
  • 2,214
  • 4
  • 24
  • 46
0

CMake doesn't do this for you:

http://public.kitware.com/Bug/view.php?id=14516

steveire
  • 10,694
  • 1
  • 37
  • 48
  • I've added further info to my answer which seems to get round the issue. – Fraser May 31 '15 at 18:43
  • I doubt this is true, but doesn't solve the problem. It would be nice, to have something like a wrapper for all the different compilers and file extensions. I use now configure_file. It is not elegant but works. – dgrat Jun 01 '15 at 07:16
  • It is true. It doesn't matter if it doesn't solve the problem. It's just the correct answer. – steveire Jun 01 '15 at 20:15