CMake - 환경 변수

mohadang·2022년 8월 7일
0

CMake

목록 보기
8/24
post-thumbnail

환경 변수

  • 환경 변수는 프로세스간 공유 가능하다.
  • cmake에서 환경 변수를 set, unset, read 할 수 있다.
  • cmake에서 생성되는 자식 프로세스에게 환경 변수가 상속 된다.

환경 변수 선언

red@DESKTOP-G15ND3V:~/cgold$ export USERNAME=ruslo
red@DESKTOP-G15ND3V:~/cgold$ echo $USERNAME
ruslo

읽기

  • $ENV{...} 문법 이용하여 환경 변수 읽을 수 있음
cmake_minimum_required(VERSION 2.8)
project(foo NONE)

# 환경 변수 읽기
message("Environment variable USERNAME: $ENV{USERNAME}")
[usage-of-variables]> rm -rf _builds
[usage-of-variables]> echo $USERNAME  # 쉘 변수
ruslo
[usage-of-variables]> cmake -H read-env -B _builds
Environment variable USERNAME: ruslo  # 접근 가능
-- Configuring done
-- Generating done

쓰기

  • set(ENV{...}) 사용하여 환경 변수 설정
cmake_minimum_required(VERSION 2.8)
project(foo NONE)
set(ENV{USERNAME} "Jane Doe") # 환경 변수 설정
message("Environment variable USERNAME: $ENV{USERNAME}")
[usage-of-variables]> rm -rf _builds
[usage-of-variables]> echo $USERNAME
ruslo
[usage-of-variables]> cmake -H set-env -B _builds
Environment variable USERNAME: Jane Doe    # 환경 변수 변경됨
-- Configuring done
-- Generating done
-- Build files have been written to: /.../usage-of-variables/_builds
  • 단 OS의 환경 변수는 여전히 그대로이다
[usage-of-variables]> echo $USERNAME
ruslo

제거

cmake_minimum_required(VERSION 2.8)
project(foo NONE)

unset(ENV{USERNAME})
message("Environment variable USERNAME: $ENV{USERNAME}")
[usage-of-variables]> rm -rf _builds
[usage-of-variables]> echo $USERNAME
ruslo
[usage-of-variables]> cmake -Hunset-env -B_builds
Environment variable USERNAME:    #  환경 변수 없어짐
-- Configuring done
-- Generating done
-- Build files have been written to: /.../usage-of-variables/_builds
  • 단 OS의 환경 변수는 여전히 그대로이다
[usage-of-variables]> echo $USERNAME
ruslo

환경 변수 상속

  • cmake 실행시 cmake 자식 프로세스는 cmake 부모 프로세스로 부터 환경 변수를 상속 받는다.
# 최상위 CMakeLists.txt : 부모 프로세스

cmake_minimum_required(VERSION 2.8)
project(foo NONE)

message("Set environment variable")

# 환경 변수 설정
set(ENV{ABC} "This is ABC")   

# ABC
message("Top level ABC: $ENV{ABC}")   

# level1 스크립트 경로
set(level1 "${CMAKE_CURRENT_LIST_DIR}/level1.cmake")

# 첫번째 level1 프로세스 실행
execute_process(    
    COMMAND "${CMAKE_COMMAND}" -P "${level1}" RESULT_VARIABLE result
)

if(NOT result EQUAL 0)
  # Error
endif()

message("Unset environment variable")

# 환경 변수 제거
unset(ENV{ABC})    

message("Top level ABC: $ENV{ABC}")   # ''

# 두번째 level1 프로세스 실행
execute_process(    
    COMMAND "${CMAKE_COMMAND}" -P "${level1}" RESULT_VARIABLE result
)

if(NOT result EQUAL 0)
  # Error
endif()
# level1 스크립트 : 자식 프로세스

# 첫번째 실행 : Environment variable from level1: This is ABC
# 두번째 실행 : Environment variable from level1:
# 부모 프로세스의 환경 변수를 그대로 상속 받음  
message("Environment variable from level1: $ENV{ABC}")

# level2 스크립트 경로
set(level2 "${CMAKE_CURRENT_LIST_DIR}/level2.cmake")

# level2 프로세스 실행
execute_process(
    COMMAND "${CMAKE_COMMAND}" -P "${level2}" RESULT_VARIABLE result
)

if(NOT result EQUAL 0)
  # Error
endif()
# level2 스크립트 : 자식의 자식 프로세스

# 첫번째 실행 : Environment variable from level1: This is ABC
# 두번째 실행 : Environment variable from level1:
# 역시 부모 프로세스의 환경 변수를 그대로 상속 받음
message("Environment variable from level2: $ENV{ABC}")

configure

  • configure 선언한 환경 변수는 빌드할때도 남는다.
# 쉘에서 환경 변수 선언 : TestValue

red@DESKTOP-G15ND3V:~/cgold$ export ABC="TestValue"
red@DESKTOP-G15ND3V:~/cgold$ echo $ABC
TestValue
cmake_minimum_required(VERSION 2.8)
project(foo NONE)

set(ENV{ABC} "123")   # cmake에서 새로운 환경 변수 값 설정 : 123

message("Environment variable ABC: $ENV{ABC}")

add_custom_target( # build 과정에 실행됨
    foo
    ALL
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_LIST_DIR}/script.cmake"
)
# script.cmake

message("Environment variable from script: $ENV{ABC}")
# 실행 : configure

[usage-of-variables]> rm -rf _builds
[usage-of-variables]> cmake -H env-configure -B _builds
Environment variable ABC: 123    # cmake에서 설정한 환경 변수 값이 출력됨
-- Configuring done
-- Generating done
-- Build files have been written to: /.../usage-of-variables/_builds
But environment variable remains the same on build step:
# 실행 : build

[usage-of-variables]> cmake --build _builds
Scanning dependencies of target foo
Environment variable from script: TestValue    # 쉘에서 설정한 환경 변수 값이 출력됨
Built target foo

# 출력 되었지만 configure 값 아닌 OS에서 설정한 값이 출력 되었다

환경 변수에 의존하는 코드

  • cmake는 환경 변수의 변경을 추적하지 않는다. 따라서 환경 변수가 변경될 가능성이 있다면 cmake 코드에서는 환경 변수 의존 코드를 사용을 피해야 한다.
  • cmake 코드가 환경 변수에 의존하고 단계적으로 코드 변경할 것을 계획한다면 워크플로우가 망가질 것이다
cmake_minimum_required(VERSION 2.8)
project(foo)
set(target_name "$ENV{ABC}-tgt") # 환경 변수를 의존하는 코드
add_executable("${target_name}" foo.cpp)
# 환경 변수 선언
[usage-of-variables]> rm -rf _builds
[usage-of-variables]> export ABC=abc

# configure
[usage-of-variables]> cmake -H env-depends -B _builds
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /.../usage-of-variables/_builds

# build
[usage-of-variables]> cmake --build _builds
Scanning dependencies of target abc-tgt
[ 50%] Building CXX object CMakeFiles/abc-tgt.dir/foo.cpp.o
[100%] Linking CXX executable abc-tgt
[100%] Built target abc-tgt     # 빌드 과정에서 환경 변수 이름을 의존하게 된다
# 환경 변수 변경

[usage-of-variables]> export ABC=123
# 다시 빌드 하였지만 변경된 환경 변수가 반영되지 않음

[usage-of-variables]> cmake --build _builds
[100%] Built target abc-tgt   # 반영되지 않음, 123으로 변경되지 않음
# 반영하기 위해서는 재 configure 해야함

[usage-of-variables]> cmake -H env-depends -B _builds
-- Configuring done
-- Generating done
-- Build files have been written to: /.../usage-of-variables/_builds

[usage-of-variables]> cmake --build _builds
Scanning dependencies of target 123-tgt
[ 50%] Building CXX object CMakeFiles/123-tgt.dir/foo.cpp.o
[100%] Linking CXX executable 123-tgt
[100%] Built target 123-tgt    # 반영됨
profile
mohadang

0개의 댓글