[CMake] Organizing source files in XCode project to match directory layout

Lane Schwartz dowobeha at gmail.com
Thu Dec 12 09:59:39 EST 2013


Hi,

I recently ported a relatively small project
(https://github.com/kpu/kenlm) to CMake. When I use the Unix Makefile
generator, everything works well as expected. Unfortunately, when I
use the XCode generator, I'm having difficulty getting the files
within the XCode project organized in the desired manner.

My goal is to have the layout of files in the XCode project match the
layout of directories and files on disk.

I have so far been unable to figure out how to do so. The directory
structure is described briefly below. I would appreciate any guidance
that anyone might have.

The project has two directories (lm and util), and each of those
directories contains two subdirectories. Each directory and
subdirectory has a CMakeLists.txt file. Each directory contains a
number of source and header files; these files are organized within
the CMakeLists.txt files into object libraries using add_library(foo
OBJECT ...). Some of the directories also contain one or more
*_main.cc files (each of these gets compiled into an executable), and
one or more *_test.cc files (each of these gets compiled into a unit
test executable).

When I use CMake to generate an XCode project, the XCode project
contains a Sources directory. Under Sources, there is one directory
for each executable, one directory for each unit test, and one
directory for each object library. Each of those directories are
further divided into a "Source Files" subdirectory, an "Object Files"
subdirectory, and a CMakeFiles.txt file. Additionally, the Sources
directory also contains three additional subdirectories: ALL_BUILD,
CTestDashboardTarget, and ZERO_CHECK.

kenlm/
  Sources/
    ALL_BUILD/
    CTestDashBoardTarget/
    ZERO_CHECK/
    bit_packing_test/
    file_piece_test/
    ...
    kenlm_util
    ...
    query/
    filter/
      Source Files/
      Object Files/
      CMakeLists.txt
    ...


By using set_target_properties(... PROPERTIES FOLDER ...), I have been
able to group some of the directories (the unit tests and the
executables) together, like so:

kenlm/
  Sources/
    ALL_BUILD/
    CTestDashBoardTarget/
    ZERO_CHECK/
    unit_tests/
      bit_packing_test/
      file_piece_test/
      ...
    kenlm_util
    ...
    executables/
      query/
      filter/
        Source Files/
        Object Files/
        CMakeLists.txt
      ...

By additionally using source_group( ... FILES ...) I have been able to
rename any of the Source Files/ subdirectories. For example, I can go
from this:

kenlm/
    executables/
      filter/
        Source Files/
        Object Files/
        CMakeLists.txt

To this:

kenlm/
    executables/
      filter/
        my_custom_src_name/
        Object Files/
        CMakeLists.txt

I want the XCode project to look more or less just like the actual
directory layout:

kenlm/
  Sources/
    lm/
      *.cc
      *.hh
      builder/
        *.cc
        *.hh
      filter/
        *.cc
        *.hh
    util/
      *.cc
      *.hh
      double_conversion/
        *.cc
        *.hh
      stream/
        *.cc
        *.hh


Based on other threads and bug reports that I've found online, I
suspect that this should be doable using source_group( ... FILES ...)
and/or set_target_properties(... PROPERTIES FOLDER ...), but I so far
haven't been able to figure out how.

Any advice or tips would be greatly appreciated.

Thanks,
Lane


More information about the CMake mailing list