Arnaud
Arnaud

Reputation: 33

Hide <project>_automoc targets in CMake

I am trying to use the CMAKE_AUTOMOC property to automatically find and compile mocable files.

However, the command set( CMAKE_AUTOMOC ON ) also includes the generated _automoc.cpp file in the Visual Studio "Source Files" filter. This is a problem for two reasons :

I would like to know if it possible to :

1) Prevent CMake from including this file to the Visual Studio filters. I searched and found https://cmake.org/Bug/print_bug_page.php?bug_id=13788. However using

SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)

did not change anything to my problem.

2) Remove a given entry from the .vcxproj.filters file using CMake, using a command similar to

source_group( "Source Files" FILES "filepath" )

which is used to add the entry "filepath" to the "Source Files" filter.

I am currently using CMake 3.5, VS 2015 and Qt 5.6. Here is a shortened version of the CMake that reproduces the problem :

project( myproj )

# Some stuff to include Qt libraries
# ...

set( CMAKE_AUTOMOC ON )

# These 2 lines don't change anything
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)

# Create project
add_executable( ${PROJECT_NAME} "main.cpp" )

In the VS filter named "Source Files", I can see main.cpp and myproj_automoc.cpp, which does not even exist before the first compilation (trying to open it with VS sends an error "Cannot open the file"). In myproj.vcxproj.filters there is an entry :

Include="C:\pathto\build\myproj_automoc.cpp">
<Filter>Source Files</Filter>

which shouldn't be here since I did not ask for it.

Am I missing something ?

Thank you for your help!

Upvotes: 3

Views: 2387

Answers (2)

Burak
Burak

Reputation: 2495

As of CMake 3.9, you can use AUTOGEN_SOURCE_GROUP to filter MOC files.

set(CMAKE_AUTOMOC ON)
set_property(GLOBAL PROPERTY AUTOGEN_SOURCE_GROUP "Generated Files")

Upvotes: 1

Atif
Atif

Reputation: 1547

I've had trouble getting this to work as documented as well. It looks like they renamed the variable in one of the releases. As of Cmake 3.0.2, you can do the following:

cmake_minimum_required(VERSION 3.0.2)
project(MyProj CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY AUTOGEN_TARGETS_FOLDER MyAutoMocFolder)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
add_executable(${PROJECT_NAME}
    ${MyProj_HEADERS}
    ${MyProj_SRCS}
    ${MyProj_QRC}
    ${MyProj_UI})

Note that you have to use set_property and the property name is now AUTOGEN_TARGETS_FOLDER.

In Xcode, this puts the generated _automoc folders in the "MyAutoMocFolder" instead of littering the parent folders with them. In Visual Studio the automoc folders in the folder as well.

It doesn't however hide the project_automoc.cpp files that are generated. To move those you have to define a source group, as Armand pointed out:

source_group( MyAutoMocFolder FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_automoc.cpp ) 

Upvotes: 3

Related Questions