Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake execute_process() always fails with "No such file or directory" when I call git

Tags:

cmake

On a linux machine, from a cmake project, I'm trying to call git using execute_process so that I can include info from source control into my app.

I created a little test to try and print the git version:

cmake_minimum_required (VERSION 2.8)

set (git_cmd "/usr/bin/git --version")
#set (git_cmd "ls") # returns success if you uncomment this line 
message(STATUS "git cmd: ${git_cmd}")
execute_process(COMMAND ${git_cmd}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  RESULT_VARIABLE git_result
  OUTPUT_VARIABLE git_ver)

message(STATUS "git ver[${git_result}]: ${git_ver}")

configure_file (
  "${PROJECT_SOURCE_DIR}/versionInfo.h.in"
  "${PROJECT_BINARY_DIR}/versionInfo.h"
  )

Which gives the following output when you run make:

-- git cmd: /usr/bin/git --version
-- git ver[No such file or directory]: 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rsanderson/build/githash: 

But if I change the command to ls the result is valid and I see the dir listing print. I also checked with which that git is indeed in /usr/bin.

Any ideas of what I'm missing here?

like image 978
Rian Sanderson Avatar asked Jul 23 '11 00:07

Rian Sanderson


2 Answers

You have to pass the arguments as a second option like this:

cmake_minimum_required (VERSION 2.8)

set (git_cmd "git")
set (git_arg "--version")
message(STATUS "git cmd: ${git_cmd}")
execute_process(COMMAND ${git_cmd} ${git_arg}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  RESULT_VARIABLE git_result
  OUTPUT_VARIABLE git_ver)

message(STATUS "git ver[${git_result}]: ${git_ver}")
like image 70
Unapiedra Avatar answered Nov 01 '22 05:11

Unapiedra


In addition to the above I'd also add the following:

OUTPUT_STRIP_TRAILING_WHITESPACE

find_package(Git QUIET)
execute_process(
    COMMAND ${GIT_EXECUTABLE} symbolic-ref --short HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE DISPLAY_GIT_BRANCH
    OUTPUT_STRIP_TRAILING_WHITESPACE)

On RedHat 8 with ninja installed I got parsing errors because of trailing whitespces

like image 1
kingkybel Avatar answered Nov 01 '22 05:11

kingkybel