-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathtars-tools.cmake
executable file
·202 lines (152 loc) · 9.55 KB
/
tars-tools.cmake
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
execute_process(COMMAND go env GOPATH OUTPUT_VARIABLE GOPATH)
string(REGEX REPLACE "\n$" "" GOPATH "${GOPATH}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/golang.cmake)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if(WIN32)
set(TARS2CPP "${GOPATH}/bin/tars2go.exe")
set(TARS_PATH "${GOPATH}/bin/tars2go.exe")
else()
set(TARS2CPP "${GOPATH}/bin/tars2go")
set(TARS_PATH "${GOPATH}/bin/tars2go")
endif()
set(TARS_WEB_HOST "" CACHE STRING "set web host")
IF (TARS_WEB_HOST STREQUAL "")
set(TARS_WEB_HOST "http://web.tars.com")
ENDIF ()
set(TARS_TOKEN "" CACHE STRING "set web token")
# set(TARS_RELEASE "${CMAKE_BINARY_DIR}/run-release.cmake")
set(TARS_UPLOAD "${CMAKE_BINARY_DIR}/run-upload.cmake")
set(TARS_TAR "${CMAKE_BINARY_DIR}/run-tar.cmake")
# FILE(WRITE ${TARS_RELEASE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo release all)\n")
FILE(WRITE ${TARS_UPLOAD} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo upload all)\n")
FILE(WRITE ${TARS_TAR} "")
function(gen_tars TARGET)
file(GLOB_RECURSE TARS_INPUT *.tars)
set(OUT_TARS_H_LIST)
if (TARS_INPUT)
foreach(TARS_FILE ${TARS_INPUT})
get_filename_component(TARS_NAME ${TARS_FILE} NAME_WE)
get_filename_component(TARS_PATH ${TARS_FILE} PATH)
set(CUR_TARS_GEN ${TARS_PATH}/${TARS_NAME}.h)
add_custom_command(
OUTPUT ${CUR_TARS_GEN}
WORKING_DIRECTORY ${TARS_PATH}
COMMAND ${TARS2CPP} -outdir tars-protocol "${TARS_TOOL_FLAG}" "${TARS_FILE}"
COMMENT "${TARS2CPP} -outdir tars-protocol ${TARS_TOOL_FLAG} ${TARS_FILE}"
DEPENDS ${TARS2CPP} ${TARS_FILE}
)
list(APPEND OUT_TARS_H_LIST ${CUR_TARS_GEN})
endforeach()
add_custom_target(${TARGET} ALL DEPENDS ${OUT_TARS_H_LIST})
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${OUT_TARS_H_LIST}")
endif()
endfunction()
#生成带tars文件的可执行程序
macro(gen_server APP TARGET)
set(UPLOAD_FILES ${ARGN})
include_directories(${PROJECT_SOURCE_DIR})
# FILE(GLOB_RECURSE SRC_FILES "*.go")
add_go_executable(${TARGET})
file(GLOB_RECURSE TARS_INPUT *.tars)
if(TARS_INPUT)
gen_tars(tars-${TARGET})
add_dependencies(${TARGET} tars-${TARGET})
endif()
#make tar #########################################################################
#must create tmp directory, avoid linux cmake conflict!
SET(RUN_TAR_COMMAND_FILE "${CMAKE_BINARY_DIR}/run-tar-${TARGET}.cmake")
FILE(WRITE ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo mkdir -p ${CMAKE_BINARY_DIR}/tmp/${TARGET})\n")
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo rm -rf ${CMAKE_BINARY_DIR}/tmp/${TARGET})\n")
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/tmp/${TARGET})\n")
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo copy bin/${TARGET} ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
IF(WIN32)
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy bin/${TARGET}.exe ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
ELSE()
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy bin/${TARGET} ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
ENDIF()
foreach(UPLOAD ${UPLOAD_FILES})
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo copy ${UPLOAD} ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
IF(IS_DIRECTORY ${UPLOAD})
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_directory ${UPLOAD} ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
ELSE()
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${UPLOAD} ${CMAKE_BINARY_DIR}/tmp/${TARGET}/)\n")
ENDIF()
endforeach(UPLOAD ${UPLOAD_FILES})
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/tmp/ tar czfv ${TARGET}.tgz ${TARGET}/)\n")
FILE(APPEND ${RUN_TAR_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/tmp/${TARGET}.tgz ${CMAKE_BINARY_DIR}/${TARGET}.tgz)\n")
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/.timestamp
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -P ${RUN_TAR_COMMAND_FILE}
DEPENDS ${TARGET}
COMMENT "tar czfv ${TARGET}.tgz")
add_custom_target(${TARGET}-tar DEPENDS ${CMAKE_BINARY_DIR}/.timestamp)
FILE(APPEND ${TARS_TAR} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -P ${RUN_TAR_COMMAND_FILE})\n")
#make upload #########################################################################
SET(RUN_UPLOAD_COMMAND_FILE "${PROJECT_BINARY_DIR}/run-upload-${TARGET}.cmake")
IF(WIN32)
FILE(WRITE ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo ${TARS_WEB_HOST}/api/upload_and_publish -Fsuse=@${TARGET}.tgz -Fapplication=${APP} -Fmodule_name=${TARGET} -Fcomment=developer-auto-upload)\n")
FILE(APPEND ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${TARS_PATH}/thirdparty/bin/curl.exe ${TARS_WEB_HOST}/api/upload_and_publish?ticket=${TARS_TOKEN} -Fsuse=@${TARGET}.tgz -Fapplication=${APP} -Fmodule_name=${TARGET} -Fcomment=developer-auto-upload)\n")
FILE(APPEND ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND -E echo \n---------------------------------------------------------------------------)\n")
ELSE()
FILE(WRITE ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo ${TARS_WEB_HOST}/api/upload_and_publish -Fsuse=@${TARGET}.tgz -Fapplication=${APP} -Fmodule_name=${TARGET} -Fcomment=developer-auto-upload)\n")
FILE(APPEND ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND curl ${TARS_WEB_HOST}/api/upload_and_publish?ticket=${TARS_TOKEN} -Fsuse=@${TARGET}.tgz -Fapplication=${APP} -Fmodule_name=${TARGET} -Fcomment=developer-auto-upload)\n")
FILE(APPEND ${RUN_UPLOAD_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo \n---------------------------------------------------------------------------)\n")
ENDIF()
#执行命令
add_custom_target(${TARGET}-upload
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${TARGET}-tar
COMMAND cmake -P ${RUN_UPLOAD_COMMAND_FILE}
COMMENT "upload ${APP}.${TARGET}.tgz and publish...")
FILE(APPEND ${TARS_UPLOAD} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -P ${RUN_UPLOAD_COMMAND_FILE})\n")
# #make release #########################################################################
# SET(RUN_RELEASE_COMMAND_FILE "${PROJECT_BINARY_DIR}/run-release-${TARGET}.cmake")
# if (TARS_INPUT)
# foreach(TARS_FILE ${TARS_INPUT})
# get_filename_component(TARS_NAME ${TARS_FILE} NAME_WE)
# get_filename_component(TARS_PATH ${TARS_FILE} PATH)
# set(CUR_TARS_GEN ${TARS_PATH}/${TARS_NAME}.h)
# if(WIN32)
# FILE(WRITE ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E make_directory c:\\tarsproto\\protocol\\${APP}\\${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo cp -rf ${CUR_TARS_GEN} c:\\tarsproto\\protocol\\${APP}\\${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CUR_TARS_GEN} c:\\tarsproto\\protocol\\${APP}\\${TARGET})\n")
# elseif(APPLE)
# FILE(WRITE ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E make_directory $ENV{HOME}/tarsproto/protocol/${APP}/${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo cp -rf ${CUR_TARS_GEN} $ENV{HOME}/tarsproto/protocol/${APP}/${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CUR_TARS_GEN} $ENV{HOME}/tarsproto/protocol/${APP}/${TARGET})\n")
# elseif(UNIX)
# FILE(WRITE ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E make_directory /home/tarsproto/${APP}/${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E echo cp -rf ${CUR_TARS_GEN} /home/tarsproto/${APP}/${TARGET})\n")
# FILE(APPEND ${RUN_RELEASE_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CUR_TARS_GEN} /home/tarsproto/${APP}/${TARGET})\n")
# endif()
# endforeach(TARS_FILE ${TARS_INPUT})
# add_custom_target(${TARGET}-release
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
# DEPENDS ${TARGET}
# COMMAND cmake -P ${RUN_RELEASE_COMMAND_FILE}
# COMMENT "call ${RUN_RELEASE_COMMAND_FILE}")
# FILE(APPEND ${TARS_RELEASE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -P ${RUN_RELEASE_COMMAND_FILE})\n")
# endif ()
endmacro()
add_custom_target(upload
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND cmake -P ${TARS_UPLOAD})
# add_custom_target(release
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
# COMMAND cmake -P ${TARS_RELEASE})
add_custom_target(tar
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND cmake -P ${TARS_TAR})
message("-------------------------------------------------------------------------------------")
message("CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message("PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message("CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message("PLATFORM: ${PLATFORM}")
message("TARS2CPP: ${TARS2CPP}")
message("TARS_WEB_HOST: ${TARS_WEB_HOST}")
message("TARS_TOKEN: ${TARS_TOKEN}")
message("-------------------------------------------------------------------------------------")