MantisBT - CMake
View Issue Details
0015794CMakeCMakepublic2015-10-15 13:122016-03-07 09:12
Davy Durham 
Gregor Jasny 
normalminoralways
closedfixed 
Xcode generator
CMake 3.0 
CMake 3.5CMake 3.5 
0015794: The Xcode generator consumes all arguments beginning with "-O" even when they're not optimization flags.
cmake, when generating Xcode projects, attempts to translate optimization (i.e. arguments starting with -O…) flags into the proper Xcode project file setting. In doing so, it removes all arguments beginning with "-O" from the CMAKE_CXX_FLAGS and CMAKE_C_FLAGS variables and they do not get passed to the OTHER_CPLUSPLUSFLAGS and OTHER_CFLAGS values in the Xcode project. However flags such as "-ObjC" and "-ObjC++" get incorrectly stripped out of the generated project.

I have attached a patch file that addresses the issue by ignoring arguments that begin with -O which are longer than 3 characters. (two files, one for cmake-3.1 and the other for git master)
For a CMakeLists.txt file containing:

  cmake_minimum_required(VERSION 3.0.2)
  project(foo)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ObjC++")
  add_executable(foo foo.cpp)

running: cmake -G Xcode <path> results in a generated foo.xcodeproj/project.pbxproj file
This file's OTHER_CPLUSPLUSFLAGS= value ought to contain -ObjC++, but it does not.
One can work around the issue by adding adding quotes around the argument which Xcode strips off but cmake doesn't check for (it technically should.. another bug?)

Example:
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \"-ObjC++\"")

This is a fragile work around and may not work as it currently does in future versions of cmake and Xcode
No tags attached.
patch cmake3_1-xcode-optimize-arg-fix.patch (3,307) 2015-10-15 13:13
https://public.kitware.com/Bug/file/5544/cmake3_1-xcode-optimize-arg-fix.patch
patch 0001-Fixed-an-issue-where-the-Xcode-generated-was-clobber.patch (3,528) 2015-10-15 13:13
https://public.kitware.com/Bug/file/5545/0001-Fixed-an-issue-where-the-Xcode-generated-was-clobber.patch
Issue History
2015-10-15 13:12Davy DurhamNew Issue
2015-10-15 13:13Davy DurhamFile Added: cmake3_1-xcode-optimize-arg-fix.patch
2015-10-15 13:13Davy DurhamFile Added: 0001-Fixed-an-issue-where-the-Xcode-generated-was-clobber.patch
2015-10-15 13:28Brad KingAssigned To => Gregor Jasny
2015-10-15 13:28Brad KingStatusnew => assigned
2015-10-15 13:28Brad KingTarget Version => CMake 3.5
2015-10-15 15:06Brad KingNote Added: 0039621
2015-10-18 16:55Gregor JasnyNote Added: 0039625
2015-10-19 11:40Davy DurhamNote Added: 0039635
2015-10-19 15:52Brad KingNote Added: 0039647
2015-10-20 08:48Ching Ping SunNote Added: 0039654
2015-10-20 16:30Gregor JasnyNote Added: 0039657
2015-10-20 16:52Gregor JasnyNote Added: 0039658
2015-10-27 13:26Brad KingNote Added: 0039684
2015-10-27 13:27Brad KingStatusassigned => resolved
2015-10-27 13:27Brad KingResolutionopen => fixed
2015-10-27 13:27Brad KingFixed in Version => CMake 3.5
2016-03-07 09:12Robert MaynardNote Added: 0040626
2016-03-07 09:12Robert MaynardStatusresolved => closed

Notes
(0039621)
Brad King   
2015-10-15 15:06   
Associated mailing list thread:

http://thread.gmane.org/gmane.comp.programming.tools.cmake.devel/14732 [^]
(0039625)
Gregor Jasny   
2015-10-18 16:55   
Hello Davy,

thank you for the bug report and the patch. Unfortunately your approach does not work with the (currently broken) handling for -Ofast. I took another route via a regular expression for the flags. I pushed my current work to the xcode-optimization-flags branch. This topic branch should fix the error but lacks some additional tests (e.g. for your -ObjC flags).

Thanks,
Gregor

PS: Topic is here: https://cmake.org/gitweb?p=stage/cmake.git;a=shortlog;h=refs/heads/xcode-optimization-flags [^]
(0039635)
Davy Durham   
2015-10-19 11:40   
Yep, I see that it never handled -Ofast properly. Your approach is probably the best it can be (a whitelist of expected values following -O, else preserve as an additional flag).

Thanks
(0039647)
Brad King   
2015-10-19 15:52   
Thanks Gregor. The regex:

 "-Ofast|-Os|-O[0-9]*"

could be something like

 "(^| )(-Ofast|-Os|-O[0-9]*)( |$)"

to avoid the boundary checks in the C++ code. Then just use the proper match index to get the content.
(0039654)
Ching Ping Sun   
2015-10-20 08:48   
Hi,Gregor,

I found the option begins with "-g", for example: "-gline-tables-only" has the same situation.(being consumed without output). Do I have to fire another issue or just report here?
(0039657)
Gregor Jasny   
2015-10-20 16:30   
Ching Ping Sun, please file a new issue for this. -g handling is much more complex than optimisation level.
(0039658)
Gregor Jasny   
2015-10-20 16:52   
I merged the topic branch to next for testing.
(0039684)
Brad King   
2015-10-27 13:26   
The fix has been integrated here:

 Xcode: Use regular expression to extract all optimisation flags
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=601e6e1a [^]
(0040626)
Robert Maynard   
2016-03-07 09:12   
Closing resolved issues that have not been updated in more than 4 months.