VERSION
- cmake의 버전에 따라 사용할 수 있는 키워드가 제한 되거나 기능 동작이 달라진다.
- 리스트 파일 맨ㄴ 처음 라인에 최소 버전 정보를 지정하여 최소 버전 이상에서만 지원하는 cmake 키워드를 사용하도록 제한할 수 있다.
# 해당 CMakeLists.txt에서는 cmake 2.8 이상에서만 지원하는 키워드를 사용할 수 있다.
cmake_minimum_required(VERSION 2.8)
# cmake 내에서 cmake version에 따라 실행할 코드를 구분할 수 있다.
if(NOT CMAKE_VERSION VERSION_LESS "3.0")
# Code with 3.0 features
endif()
# cmake 프로그램 버전 확인
$ cmake --version
버전에 맞지 않는 코드 사용 예
cmake_minimum_required(VERSION 2.8)
# cmake_minimum_required(VERSION 3.0)
project(foo)
add_library(foo foo.cpp)
# BAD CODE! CMake 3.0(policy CMP0038) 부터 적용되는 정책으로는 동작하지 않는 코드
# 2.8 과거 버전은 동작 가능
target_link_libraries(foo foo)
# 2.8 cmake
[policy-examples]> cmake --version
cmake version 2.8.12.2
[policy-examples]> rm -rf _builds
[policy-examples]> cmake -H bug-2.8 -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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /.../policy-examples/_builds
제대로 동작
# 3.0 cmake
[policy-examples]> cmake --version
cmake version 3.5.2
[policy-examples]> rm -rf _builds
[policy-examples]> cmake -H bug-2.8 -B _builds
...
-- Configuring done
CMake Warning (dev) at CMakeLists.txt:4 (add_library): # 경고 출력
Policy CMP0038 is not set: Targets may not link directly to themselves.
Run "cmake --help-policy CMP0038" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
Target "foo" links to itself.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /.../policy-examples/_builds
경고를 출력한다.
- CMakeLists.txt에서 cmake 최소 버전을 VERSION 3.0 이상으로 지정
cmake_minimum_required(VERSION 3.0)
[policy-examples]> rm -rf _builds
[policy-examples]> cmake -H set-3.0 -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
CMake Error at CMakeLists.txt:4 (add_library): # configuration 단계에서 에러 발생
Target "foo" links to itself.
3.0에서 동작하지 않는 cmake 키워드를 사용하여 에러가 발생한다.
CMake 정책
# cmake에 정책 설정
cmake_minimum_required(VERSION 2.8)
project(foo)
cmake_policy(SET CMP0038 OLD) # 정책 설정
add_library(foo foo.cpp)
# BAD CODE! CMake 3.0(policy CMP0038) 부터 적용되는 정책으로는 동작하지 않는 코드
# 2.8 과거 버전은 동작 가능,
target_link_libraries(foo foo)
# cmake 3.0 에서 실행
[policy-examples]> cmake --version
cmake version 3.5.2
[policy-examples]> rm -rf _builds
[policy-examples]> cmake -H unknown-2.8 -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
3.5.2에서 실행하지만 에러가 없어졌다.
최신 버전의 cmake로 사용하면서 경고를 없애고 싶을때 사용
# cmake 2.8에서는 CMP0038 키워드가 존재하지 않아서 에러가 발생
> cmake --version
cmake version 2.8.12.2
> rm -rf _builds
> cmake -H unknown-2.8 -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
-- 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
CMake Error at CMakeLists.txt:4 (cmake_policy): <-- 에러 발생
Policy "CMP0038" is not known to this version of CMake.
-- Configuring incomplete, errors occurred!
과거 버전 cmake에서는 CMP0038 정책을 알 수 없기에 에러 발생
이럴때는 CMP0038 키워드를 사용할 수 있는 최신 cmake 버전으로 실행할때만 정책을 설정하도록 변경 해야함
- cmake 2.8은 cmake_policy 지정할 필요 없음으로 회피
cmake_minimum_required(VERSION 2.8)
project(foo)
if(POLICY CMP0038) # CMP0038일 때만 정책 설정
# Policy CMP0038 introduced since CMake 3.0 so if we want to be compatible
# with 2.8 (see cmake_minimum_required) we should put 'cmake_policy' under
# condition.
cmake_policy(SET CMP0038 OLD)
endif()
add_library(foo foo.cpp)
target_link_libraries(foo foo) # BAD CODE! Make no sense