MantisBT - CMake
View Issue Details
0015321CMakeCMakepublic2014-12-20 19:122016-06-10 14:31
Denis Denisov 
Kitware Robot 
normalminoralways
closedmoved 
DarwinMac OS X10.10.1
CMake 3.0.2 
 
0015321: Ninja: Custom command lines do not escape "$"
`ninja: error: build.ninja:3194: bad $-escape (literal $ must be written as $$)``

before code:
```
  # Xcode/Ninja generators undefined MAKE
  if(NOT MAKE)
    set(MAKE make)
  endif()

  add_custom_target(
    hack
    ALL
    COMMAND
      env OPTBIN="${OCAMLC_OPT_SUFFIX}"
          $(MAKE) EXTRA_INCLUDE_PATHS="${extra_include_paths}"
          EXTRA_LIB_PATHS="${extra_lib_paths}"
          EXTRA_CC_FLAGS="${extra_cc_flags}"
          EXTRA_NATIVE_LIBRARIES="${extra_native_libraries}"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src"
  )
```


code build.ninja:3194:
```
#############################################
# Custom command for hphp/hack/CMakeFiles/hack

build hphp/hack/CMakeFiles/hack: CUSTOM_COMMAND || third-party/lz4/liblz4.a
  COMMAND = cd /home/denji/Desktop/hhvm/hphp/hack/src && env OPTBIN=".opt" $(MAKE) EXTRA_INCLUDE_PATHS="/usr/include /home/denji/Desktop/hhvm/third-party/lz4" EXTRA_LIB_PATHS="/usr/lib/x86_64-linux-gnu /home/denji/Desktop/hhvm/third-party/lz4" EXTRA_CC_FLAGS="-DHAVE_ELF_GETSHDRSTRNDX -pthread" EXTRA_NATIVE_LIBRARIES="lz4"
``
No tags attached.
Issue History
2014-12-20 19:12Denis DenisovNew Issue
2014-12-20 19:12Denis DenisovNote Added: 0037504
2014-12-20 19:15Denis DenisovNote Edited: 0037504bug_revision_view_page.php?bugnote_id=37504#r1650
2014-12-22 09:10Brad KingNote Added: 0037505
2014-12-22 09:11Brad KingStatusnew => backlog
2014-12-22 09:11Brad KingSummaryGeneration of the ninja, ignore escape => Ninja: Custom command lines do not escape "$"
2014-12-22 09:18Denis DenisovNote Added: 0037506
2014-12-22 09:19Denis DenisovNote Edited: 0037506bug_revision_view_page.php?bugnote_id=37506#r1652
2015-07-02 15:27Denis DenisovNote Added: 0039014
2015-07-02 15:29Denis DenisovNote Added: 0039015
2015-07-02 15:29Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1818
2015-07-02 15:29Denis DenisovNote Deleted: 0039015
2015-07-02 15:31Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1819
2015-07-02 15:31Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1820
2015-07-02 15:31Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1821
2015-07-02 15:31Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1822
2015-07-02 15:32Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1823
2015-07-02 15:32Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1824
2015-07-02 15:32Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1825
2015-07-02 15:33Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1826
2015-07-02 15:33Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1827
2015-07-02 15:41Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1828
2015-07-02 15:54Denis DenisovNote Edited: 0039014bug_revision_view_page.php?bugnote_id=39014#r1829
2015-07-06 14:31Brad KingNote Added: 0039019
2015-07-06 14:34Brad KingNote Added: 0039020
2015-07-06 14:37Denis DenisovNote Added: 0039021
2015-07-06 14:56Brad KingNote Added: 0039022
2015-07-06 15:17Denis DenisovNote Added: 0039023
2015-07-06 15:17Denis DenisovNote Added: 0039024
2015-07-06 15:17Denis DenisovNote Edited: 0039023bug_revision_view_page.php?bugnote_id=39023#r1831
2015-07-06 15:17Denis DenisovNote Deleted: 0039024
2015-07-06 15:17Denis DenisovNote Edited: 0039023bug_revision_view_page.php?bugnote_id=39023#r1832
2015-07-06 15:18Denis DenisovNote Edited: 0039023bug_revision_view_page.php?bugnote_id=39023#r1833
2015-07-06 15:35Brad KingNote Added: 0039025
2016-06-06 13:37Brad KingView Statusprivate => public
2016-06-10 14:29Kitware RobotNote Added: 0042694
2016-06-10 14:29Kitware RobotStatusbacklog => resolved
2016-06-10 14:29Kitware RobotResolutionopen => moved
2016-06-10 14:29Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0037504)
Denis Denisov   
2014-12-20 19:12   
(edited on: 2014-12-20 19:15)
https://github.com/facebook/hhvm/blob/c63a8efb/hphp/hack/CMakeLists.txt#L34-L49 [^]

(0037505)
Brad King   
2014-12-22 09:10   
For your particular example, the "$(MAKE)" reference is not evaluated by CMake but simply passed through. I think you want something more like:

  # Xcode/Ninja generators undefined MAKE
  if(CMAKE_GENERATOR MATCHES "Make")
    set(MAKE "$(MAKE)")
  else()
    set(MAKE make)
  endif()

  add_custom_target(
    hack
    ALL
    COMMAND
      env OPTBIN="${OCAMLC_OPT_SUFFIX}"
          "${MAKE}" EXTRA_INCLUDE_PATHS="${extra_include_paths}"
          EXTRA_LIB_PATHS="${extra_lib_paths}"
          EXTRA_CC_FLAGS="${extra_cc_flags}"
          EXTRA_NATIVE_LIBRARIES="${extra_native_libraries}"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src"
  )


However, the Ninja generator may still need to be corrected to escape "$" as "$$" inside command lines.
(0037506)
Denis Denisov   
2014-12-22 09:18   
(edited on: 2014-12-22 09:19)
Please note, there are 2 types of builds using cmake and pure GNU make (CMAKE_GENERATOR - not support the Makefile)

cmake can be self-contained or collected directly by using the Makefile

(0039014)
Denis Denisov   
2015-07-02 15:27   
(edited on: 2015-07-02 15:54)
Problem when using ninja generation in cmake is that it is not enough screens to a $MAKE variable

diff:
- $(MAKE)
+ $$(MAKE)

or
+ $MAKE

#debian:/home/denji/kitchen_sync/build# ninja
ninja: error: build.ninja:338: bad $-escape (literal $ must be written as $$)
  COMMAND = cd /home/denji/kitchen_sync/build/src/yaml-cpp && $(MAKE) cl...
                                                              ^ near here

# cmake --version
cmake version 3.0.2

# ninja --version
1.3.4

# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.1 (jessie)
Release: 8.1
Codename: jessie

# git clone https://github.com/willbryant/kitchen_sync.git [^]

#debian:/home/denji/kitchen_sync/build# cmake -G Ninja ..
-- The CXX compiler identification is GNU 4.9.2
-- Check for working CXX compiler using: Ninja
-- Check for working CXX compiler using: Ninja -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Performing Test COMPILER_SUPPORTS_CXX11
-- Performing Test COMPILER_SUPPORTS_CXX11 - Success
-- Performing Test COMPILER_SUPPORTS_CXX0X
-- Performing Test COMPILER_SUPPORTS_CXX0X - Success
-- Performing Test COMPILER_SUPPORTS_STDLIB
-- Performing Test COMPILER_SUPPORTS_STDLIB - Failed
-- Boost version: 1.55.0
-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
-- The C compiler identification is GNU 4.9.2
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Boost version: 1.55.0
-- Performing Test FLAG_WEXTRA
-- Performing Test FLAG_WEXTRA - Success
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libssl.so;/usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.0.1k")
-- Found MySQL: /usr/lib/libmysqlclient.so
-- Could NOT find PostgreSQL (missing: PostgreSQL_TYPE_INCLUDE_DIR) (found version "9.4.3")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/denji/kitchen_sync/build


#debian:/home/denji/kitchen_sync/build# ninja
ninja: error: build.ninja:338: bad $-escape (literal $ must be written as $$)
  COMMAND = cd /home/denji/kitchen_sync/build/src/yaml-cpp && $(MAKE) cl...
                                                              ^ near here

(0039019)
Brad King   
2015-07-06 14:31   
With the VERBATIM option to add_custom_command, CMake does correctly escape "$" in custom commands. The test suite covers it here:

 http://www.cmake.org/gitweb?p=cmake.git;a=blob;f=Tests/CustomCommand/CMakeLists.txt;hb=v3.3.0-rc3#l271 [^]

The problem here is that there is a special case in the escaping rules to allow Makefile variable references like "$(VAR)" to work without escaping the "$" sign. See the SetEscapeAllowMakeVars call here:

 http://www.cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmMakefile.cxx;hb=v3.3.0-rc3#l960 [^]
(0039020)
Brad King   
2015-07-06 14:34   
In your use case, when Ninja launches the command line containing a literal "$$(MAKE)" (thus properly escaped), what do you expect to happen? Ninja won't replace that with anything. Then the shell will see "$(MAKE)" and treat it as a recursive shell call looking for a command line tool called "MAKE" to run with no arguments.
(0039021)
Denis Denisov   
2015-07-06 14:37   
I don't want to manually edit ninja file, clearly understood that the escape problem caused in cmake.

We will wait for your proposed solutions around the problem.

Thanks.
(0039022)
Brad King   
2015-07-06 14:56   
Re 0015321:0039021: What I'm asking in 0015321:0039020 is what you expect to happen if CMake were to properly escape the "$" in this case. Consider this session:

$ cat test.ninja
rule R
  command = echo $$(MAKE)
build all: R

$ ninja -f test.ninja
[1/1] echo $(MAKE)
/bin/sh: 1: MAKE: not found
(0039023)
Denis Denisov   
2015-07-06 15:17   
(edited on: 2015-07-06 15:18)
$$(MAKE) work fine

$ ninja -f test.ninja
[1/1] echo $(MAKE)
/bin/sh: 1: MAKE: not found

-------------

$MAKE work fine, no error signal

$ ninja -f test.ninja
[1/1] echo

(0039025)
Brad King   
2015-07-06 15:35   
Re 0015321:0039023: Right, so I'm saying that having "$$(MAKE)" in the build.ninja file is incorrect for this use case. It won't actually run "make". The CMake code

  # Xcode/Ninja generators undefined MAKE
  if(NOT MAKE)
    set(MAKE make)
  endif()

does not set anything meaningful to Ninja at build time.

Have you tried the code I posted in 0015321:0037505? AFAICT it will achieve your goals.
(0042694)
Kitware Robot   
2016-06-10 14:29   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.