CMake/Xcode build fails trying to build asan

Sadly, that doesn't work.

I think the root problem is that Xcode automatically specifies -isysroot
and -mmacosx-version-min based on the $(SDKROOT) and
$(MACOSX_DEPLOYMENT_TARGET) project settings. It sticks these flags
_before_ those specified by the $(OTHER_CFLAGS) project setting, which
is where the values of the DARWIN_osx_FLAGS and DARWIN_ios_CFLAGS CMake
variables wind up.

Inspecting the RTSanitizerCommon.iossim target in Xcode, the $(SDKROOT)
project setting points at the location of my OS X SDK on disk. This is
why the build command includes both the "-isysroot …/MacOSX10.9.sdk"
(which comes from $(SDKROOT)) and "-isysroot …/iphonesimulator7.0.sdk"
(which comes from $(OTHER_CXXFLAGS)) settings.

I think the solution requires not putting the -isysroot and
-mmacosx-version-min flags in OTHER_CFLAGS when using the Xcode
generator. Instead, the ios targets should set the $(SDKROOT) and
$(MACOSX_DEPLOYMENT_TARGET) build settings in the generated pbxproj. I
took a first pass at that:
<https://github.com/kylesluder/objc-namespaces/commit/46fee77b6b8687ca463e6bde07d61ce27fd975e1>

Unfortunately, this doesn't seem to successfully set the $(SDKROOT) to
iphonesimulator on the ios targets.

On a side note, I don't know _why_ it's building the iPhone Simulator
target even though I specified '-DLLVM_TARGETS_TO_BUILD="X86"' on the
command line.

--Kyle Sluder

CMake has builtin features to generate these flags in the appropriate
place for each generator. See CMAKE_OSX_SYSROOT and
CMAKE_OSX_DEPLOYMENT_TARGET:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_SYSROOT.rst;hb=28dcccfa
http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst;hb=28dcccfa

Note the disclaimer about when they should be set:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_VARIABLE.txt;hb=28dcccfa

-Brad

> I think the solution requires not putting the -isysroot and
> -mmacosx-version-min flags in OTHER_CFLAGS when using the Xcode
> generator. Instead, the ios targets should set the $(SDKROOT) and
> $(MACOSX_DEPLOYMENT_TARGET) build settings in the generated pbxproj.

CMake has builtin features to generate these flags in the appropriate
place for each generator. See CMAKE_OSX_SYSROOT and
CMAKE_OSX_DEPLOYMENT_TARGET:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_SYSROOT.rst;hb=28dcccfa
http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst;hb=28dcccfa

Note the disclaimer about when they should be set:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Help/variable/CMAKE_OSX_VARIABLE.txt;hb=28dcccfa

Hi Brad,

From that disclaimer, it sounds like CMake is treating the SDKROOT as

some sort of per-machine global. That is not how these settings are
intended to be used; the SDK and deployment target can be independently
configured for each target in a project, as well as for the project as a
whole. It can also be inherited from any .xcconfig files attached to the
target or the project.

You might not have seen my thread on the CMake mailing list regarding
this: <http://www.cmake.org/pipermail/cmake/2014-February/057038.html>

It is quite common to have a single .xcodeproj that contains two
targets: one for iOS and one for Mac. The same source files are members
of both targets. Through some mechanism I haven't devised yet, Xcode can
distinguish targets that were created from the iOS template or the Mac
template, and sets the root of their settings cascade to the appropriate
platform. This includes the SDKROOT setting.

Am I misunderstanding the disclaimer? If not, I'm afraid it is
ill-defined and does not do what is necessary here.

--Kyle Sluder

Okay, I misread the use case here. The CMAKE_OSX_SYSROOT and
CMAKE_OSX_ARCHITECTURES were created to support builds of OS X
universal binaries where everything was still targeting the same
set of platforms (with a single -isysroot for everything).

CMake has no official support for mixed-platform builds so you will
either have to hack the build rules or help improve CMake to
support this. There is already support for a per-binary list of
architectures:

http://www.cmake.org/cmake/help/v2.8.12/cmake.html#prop_tgt:OSX_ARCHITECTURES

Something similar could be created for the other settings.

-Brad

> It is quite common to have a single .xcodeproj that contains two
> targets: one for iOS and one for Mac.

Okay, I misread the use case here. The CMAKE_OSX_SYSROOT and
CMAKE_OSX_ARCHITECTURES were created to support builds of OS X
universal binaries where everything was still targeting the same
set of platforms (with a single -isysroot for everything).

That's unfortunate. :frowning:

CMake has no official support for mixed-platform builds so you will
either have to hack the build rules or help improve CMake to
support this. There is already support for a per-binary list of
architectures:

http://www.cmake.org/cmake/help/v2.8.12/cmake.html#prop_tgt:OSX_ARCHITECTURES

Something similar could be created for the other settings.

I'm interested in helping make this happen, but I have no idea what the
LLVM project's policies are regarding minimum CMake versions. Can
someone provide insight here?

--Kyle Sluder

I think the first step would be to get the feature working in upstream
CMake. That experience may help guide construction of a temporary
workaround for use in llvm while waiting for the feature to be in a
sufficiently mature release of CMake.

-Brad