# CMake examples in Xcode

**URL:** <https://forum.juce.com/t/cmake-examples-in-xcode/42188>\
**Category:** General JUCE discussion\
**Created:** [October 12, 2020, 5:54pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188 "2020-10-12T17:54:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 12, 2020, 5:54pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/1 "2020-10-12T17:54:50Z")

</div>

Digging into CMake for the first time. Very excited about it, but had some questions about the Xcode project the example config produces.

I’m hoping to use CMake on CI and would like to use the built Xcode project locally, however it’s a bit cluttered with the default config it seems:

1. The example CMake projects in the JUCE repo don’t add headers ( `PluginEditor.h` and `PluginProcessor.h`) to `target_sources` — not sure if this is intentional, but without listing them there, they don’t show up in Xcode for me, so I assume it’s an omission?

2. Headers and Source are output into two separated folders — is this trivial to change? I’d like to have my source organized the way Projucer had it / the way it’s in source control.

3. Is there a way to stick all the juce prefixed .mm and .h files in a subfolder to get them out of the way?

Thanks for any pointers!

 ![Screenshot 2020-10-12 PluginEditor.cpp - Xcode](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/8/8453f34a09bff87cfc0f4de7cc249128cc40454a.jpeg)

---

<div class="post-metadata">

**Author:** ![denvercoder9](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@denvercoder9](https://forum.juce.com/u/denvercoder9)\
**Post date:** [October 13, 2020, 2:53pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/2 "2020-10-13T14:53:05Z")

</div>

New to CMake as well, but from what ive seen the source files list should really only contain source files (.cpp) that get compiled. If you want your header file to be found by xcode without providing the path to it in your include statement, you can add the directory it is in as an include directory:

```auto
target_include_directories(YOUR_TARGET PUBLIC
    DirectoryYourHeaderIsIn/
)

```

im on linux but I dont have all those juce module files in my source directory, not sure but that could be due to the way you are including juce in your cmake lists. How have you included it? Are you using cmake to find the library or have you got a juce subdirectory and are using add\_subdirectory? It also could just be the way the xcode generator works. Like I said im on linux so im not too sure.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 13, 2020, 3:42pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/3 "2020-10-13T15:42:50Z")

</div>

Hey there!

My CMakeLists.txt is here:

> <https://github.com/sudara/pamplejuce/blob/main/CMakeLists.txt>

Which is essentially a derivative of the [JUCE example for an Audio Plugin](https://github.com/juce-framework/JUCE/blob/master/examples/CMake/AudioPlugin/CMakeLists.txt) plus a few things I ran into on the forum.

> [@denvercoder9](#):
>
> you can add the directory it is in as an include directory:

Thanks! That’s helpful, my next step is to look into customizing things beyond the example config.

> [@denvercoder9](#):
>
> It also could just be the way the xcode generator works.

Yeah… I’m mainly wondering if the example CMakeLists.txt are just not (yet?) optimized for Xcode or if I’m missing something / my expectations are off…

Appreciate the reply!

---

<div class="post-metadata">

**Author:** ![denvercoder9](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@denvercoder9](https://forum.juce.com/u/denvercoder9)\
**Post date:** [October 13, 2020, 3:47pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/4 "2020-10-13T15:47:03Z")

</div>

So looking at your cmake list, you have enabled module source groups. The api docs say this:

> JUCE\_ENABLE\_MODULE\_SOURCE\_GROUPS
> 
> This option controls whether dummy targets are added to the build, where these targets contain all of the source files for each module added with juce\_add\_module(s). If you’re planning to use an IDE and want to be able to browse all of JUCE’s source files, this may be useful. However, it will increase the size of generated IDE projects and might slow down configuration a bit. If you enable this, you should probably also add set\_property(GLOBAL PROPERTY USE\_FOLDERS YES) to your top level CMakeLists, otherwise the module sources will be added directly to the top level of the project, instead of in a nice ‘Modules’ subfolder.

In your cmake file you commented out the use folders set\_property, so it looks like thats why the modules are all being added at the top level. Try uncommenting that and see if the modules get placed in their own folder:

```auto
set_property(GLOBAL PROPERTY USE_FOLDERS YES)
option(JUCE_ENABLE_MODULE_SOURCE_GROUPS "Show all module sources in IDE projects" ON)

```

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 13, 2020, 3:53pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/5 "2020-10-13T15:53:43Z")

</div>

Hey sorry @denvercoder9 — Apparently I left the github version of the repo in a messy state while I was troubleshooting. I caught this right after posting, but you were too quick! It’s updated now to the original config I wanted.

I tried building with and without JUCE\_ENABLE\_MODULE\_SOURCE\_GROUPS (blowing away the builds folder in between) as well as with and without the USE\_FOLDERS in attempts to understand what was happening… no luck…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 13, 2020, 4:45pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/6 "2020-10-13T16:45:49Z")

</div>

I tried to use `source_group(TREE)` as described here so that I can have CMake output source groups that mimic my actual source (my goal): [https://cliutils.gitlab.io/modern-cmake/chapters/features/ides.html](https://cliutils.gitlab.io/modern-cmake/chapters/features/ides.html)

However, not having much luck getting anything to show up that way. Next stop is looking into the cmake utils that juce provides, I think…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 28, 2020, 12:06am UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/7 "2020-10-28T00:06:30Z")

</div>

Followup on cleaning up the IDE presentation.

In the end I took an approach [similar to what Tracktion’s pluginval does](https://github.com/Tracktion/pluginval/blob/develop/CMakeLists.txt#L43-L62) and have a more usable IDE-friendly xcode build with:

```auto
set(SourceFiles
	Source/PluginEditor.h
	Source/PluginProcessor.h
	Source/PluginEditor.cpp
	Source/PluginProcessor.cpp)

target_sources(Pamplejuce PRIVATE ${SourceFiles})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/Source PREFIX Source FILES ${SourceFiles})

```

 ![Screenshot 2020-10-27 Pamplejuce.xcodeproj - Xcode](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/f/f24d0ccf0158b12a2befb34c3bd6002b7c9dc39d.jpeg)

This still felt pretty messy, so I’ve manually moved all the other targets into a folder called “Targets”:

```auto
# https://cliutils.gitlab.io/modern-cmake/chapters/features/ides.html
set_property(TARGET Pamplejuce_AU PROPERTY FOLDER "Targets")
set_property(TARGET Pamplejuce_AUv3 PROPERTY FOLDER "Targets")
set_property(TARGET Pamplejuce_VST3 PROPERTY FOLDER "Targets")
set_property(TARGET Pamplejuce_All PROPERTY FOLDER "Targets")
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "Targets")
set(CMAKE_SUPPRESS_REGENERATION true) # No ZERO_CHECK

```

I did this manually because `set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "Targets")` doesn’t seem to work (or is maybe overridden by JUCEUtils.cmake)?

Looking better:

 ![Screenshot 2020-10-28 Pamplejuce.xcodeproj - Xcode](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/b/bad7e3d8b65cae04a28834a6581c0abd33633f54.jpeg)

Next up I plan on looking into:

1. Consolidating/nesting/removing the “Source Files” and “Header Files” directories. These seem to be default cmake groupings but I haven’t figured out if it would require changes to JUCEUtils.cmake
2. Setting some scheme options by default, for example having the AudioPluginHost being the executable: [https://cmake.org/cmake/help/latest/prop\_tgt/XCODE\_GENERATE\_SCHEME.html](https://cmake.org/cmake/help/latest/prop_tgt/XCODE_GENERATE_SCHEME.html)
3. Figuring out if I can remove the module schemes that get added when JUCE\_ENABLE\_MODULE\_SOURCE\_GROUPS is on.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [October 28, 2020, 8:48am UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/8 "2020-10-28T08:48:27Z")

</div>

So cool, let’s hope the JUCE team has a look at this!

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [October 28, 2020, 10:35pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/9 "2020-10-28T22:35:07Z")

</div>

@yfede Thanks!

Here are a few more Xcode sanity bringers:

`JUCE_ENABLE_MODULE_SOURCE_GROUPS` is great, but adds Xcode schemes which makes things very noisy:

 ![Screenshot 2020-10-28 Pamplejuce.xcodeproj - Xcode](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/7/7699298aeaa4b4d2a5dff0ec9cb50a4dcd13bf13.jpeg)

So I turned off scheme generation globally:

```auto
set(CMAKE_XCODE_GENERATE_SCHEME OFF)

```

And then selectively turned it on for the Plugin targets:

```auto
set_target_properties(Pamplejuce_AU Pamplejuce_AUv3 Pamplejuce_VST3 Pamplejuce_All PROPERTIES 
	FOLDER "Targets"
	XCODE_GENERATE_SCHEME ON)

```

But it would probably be nicer to have these properties set in JUCEUtils.cmake (maybe in `_juce_add_interface_library`)?

To reduce noise from the default “Header Files” and “Source Files” folders, I’m using a shotgun approach:  
`source_group("JUCE Library Code" REGULAR_EXPRESSION "juce_")`

This would probably be nicer also handled in JUCEUtils.cmake (but not sure where…)

Current status:

1. Still can’t seem to stick ALL\_BUILD in Targets
2. Still has duplicate nesting setup by JUCEUtils.cmake

But looking nicer! [Current CMakeLists.txt](https://github.com/sudara/pamplejuce/blob/main/CMakeLists.txt)

 ![Screenshot 2020-10-28 Pamplejuce.xcodeproj - Xcode](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/d/d096cb7d094b41fd27b4641acd48c0b6b7ff418e.jpeg)

---

<div class="post-metadata">

**Author:** ![frye](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/frye/32/9839_2.png) [@frye](https://forum.juce.com/u/frye)\
**Post date:** [November 10, 2020, 2:55pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/10 "2020-11-10T14:55:22Z")

</div>

> [@sudara](#):
>
> folder

Yeah the folder were just naggin’ at me all day. I was just about to embark on another journey through the CMake forest to find that you have already taken it upon yourself. Thank you!

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 10, 2020, 5:01pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/11 "2020-11-10T17:01:24Z")

</div>

@frye Awesome! I spent some more time on folders recently but couldn’t figure out how to get rid of that extra folder named after the target (Pamplejuce \> Pamplejuce in my example).

I confirmed that `PROJECT_SOURCE_DIR` as well as the target’s `SOURCE_DIR` don’t have that extra folder. Also inspected via `get_target_property(MY_PROJECT_SOURCES Pamplejuce SOURCES)` and everything seems correct…

Turning off `USE_FOLDERS` or having it on and `set_target_properties(Pamplejuce PROPERTIES FOLDER "")` still leaves a top level target folder in Xcode called “Pamplejuce” so maybe I’m still missing something and will have to learn to love that nested lyfe.

---

<div class="post-metadata">

**Author:** ![frye](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/frye/32/9839_2.png) [@frye](https://forum.juce.com/u/frye)\
**Post date:** [November 10, 2020, 6:56pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/12 "2020-11-10T18:56:53Z")

</div>

Haha, well I took a CMake journey a couple months back and had a pretty good setup, after building it looked like this but with no juce source files in there:  
[CMakeLists.txt](https://forum.juce.com/uploads/short-url/op3VnWBSzJj6hfrdtw1NGZo6FIZ.txt) (6.7 KB)

 ![Screen Shot 2020-11-10 at 12.35.21 PM](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/a/abc7c8e6f08d05038e9f7d3bf1c954807b8a3f18.jpeg)  
After commenting out the generate\_juce\_header those popped up. Your changes got those out of there and fixed a couple other things but the .mm files are now in the “Target Membership” list on the right… I attached the old file I had worked out if you want to check that out.

One thing, I haven’t noticed anyone use the IF(WIN32) / IF(UNIX) type statements in any juce examples, do you know if this is considered a bad idea or something?

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [November 10, 2020, 7:45pm UTC](https://forum.juce.com/t/cmake-examples-in-xcode/42188/13 "2020-11-10T19:45:39Z")

</div>

It’s totally fine to use if statements.

I think the JUCE examples didn’t use them just because they’re trying the ‘user’ script to be as clean and portable as possible, and for simple/personal projects you might not need platform specific configuration as JUCE took care of most of it.
