-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFBGenCMakeBuildInfo.cmake
More file actions
106 lines (99 loc) · 4.19 KB
/
Copy pathFBGenCMakeBuildInfo.cmake
File metadata and controls
106 lines (99 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Adds generation of build info as library to the project.
# The library name is supplied as argument. After the library is linked with,
# the code can use variables defined in `neteng/fboss/bgp/cpp/common/CMakeBuildInfo.h`.
# This function will generate `GenBuildInfo.txt` file in binaries folder
# and add execution of `cmake -P <binary dir>/GenBuildInfo.txt` during build
# which will produce and compile C++ source code with build info and link it
# into static library.
function(add_build_info LIB_NAME)
set(cmake_file "${CMAKE_BINARY_DIR}/build_info/GenBuildInfo.txt")
set(cpp_file "${CMAKE_BINARY_DIR}/build_info/CMakeBuildInfo.cpp")
file(WRITE ${cmake_file}
"# Generated by CMake, don't change, changes will be overwritten.\n"
"set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR} ${CMAKE_MODULE_PATH})\n"
"include(FBGenCMakeBuildInfo)\n"
"generate_build_info(\n"
" \"${cpp_file}\"\n"
" \"${CMAKE_SOURCE_DIR}\"\n"
" \"${CMAKE_BUILD_TYPE}\"\n"
" \"${CMAKE_SYSTEM_NAME}\"\n"
")"
)
add_custom_command(
OUTPUT
${cpp_file}
MAIN_DEPENDENCY
${cmake_file}
WORKING_DIRECTORY
${CMAKE_SOURCE_DIR}
COMMAND
"${CMAKE_COMMAND}" -P ${cmake_file}
)
add_library(${LIB_NAME} OBJECT ${cpp_file})
endfunction()
function(generate_build_info FILE_PATH BUILD_PATH BUILD_MODE BUILD_PLATFORM)
find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
WORKING_DIRECTORY ${BUILD_PATH}
RESULT_VARIABLE git_rev_parse_rc
OUTPUT_VARIABLE build_rev
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${git_rev_parse_rc} EQUAL 0)
execute_process(COMMAND ${GIT_EXECUTABLE} show -s --format=%ct ${build_rev}
WORKING_DIRECTORY ${BUILD_PATH}
RESULT_VARIABLE git_show_unixtime_rc
OUTPUT_VARIABLE build_rev_unixtime
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT ${git_show_unixtime_rc} EQUAL 0)
message(WARNING "Git show failed, build will not contain rev date.")
set(build_rev_unixtime "0")
endif()
else()
message(WARNING
"Git rev-parse failed, build will not contain git hash.")
set(build_rev "unknown")
set(build_rev_unixtime "0")
endif()
else()
set(build_rev "unknown")
set(build_rev_unixtime "0")
endif()
if(DEFINED ENV{USER})
set(build_user $ENV{USER})
else()
set(build_user "unknown")
endif()
cmake_host_system_information(RESULT build_hostname QUERY FQDN)
set(build_tool ${CMAKE_COMMAND})
string(TIMESTAMP build_datetime UTC)
string(TIMESTAMP build_unixtime "%s" UTC)
file(WRITE ${FILE_PATH}
"// Generated by CMake, don't change, changes will be overwritten.\n"
"#include <stdint.h>\n"
"extern const char* const BuildInfo_kUser = \"${build_user}\";\n"
"extern const char* const BuildInfo_kTime = \"${build_datetime}\";\n"
"extern const uint64_t BuildInfo_kTimeUnix = ${build_unixtime};\n"
"extern const char* const BuildInfo_kHost = \"${build_hostname}\";\n"
"extern const char* const BuildInfo_kPath = \"${BUILD_PATH}\";\n"
"extern const char* const BuildInfo_kRevision = \"${build_rev}\";\n"
"extern const uint64_t BuildInfo_kRevisionCommitTimeUnix = ${build_rev_unixtime};\n"
"extern const char* const BuildInfo_kPlatform = \"${BUILD_PLATFORM}\";\n"
"extern const char* const BuildInfo_kBuildTool = \"${build_tool}\";\n"
"extern const char* const BuildInfo_kBuildMode = \"${BUILD_MODE}\";\n"
)
message("Generated build info: ${FILE_PATH}")
endfunction()