From c937df33ef09eb98d9a276200e76fb8ee257fb15 Mon Sep 17 00:00:00 2001 From: kervala Date: Tue, 6 Mar 2012 14:21:23 +0100 Subject: [PATCH] Changed: #1444 Add details for user-agent --- code/CMakeLists.txt | 1 + code/CMakeModules/FindMercurial.cmake | 108 ++++++++++++++++++++++++++ code/CMakeModules/GetRevision.cmake | 44 +++++++++++ code/CMakeModules/nel.cmake | 27 +++++++ code/revision.h.in | 7 ++ 5 files changed, 187 insertions(+) create mode 100644 code/CMakeModules/FindMercurial.cmake create mode 100644 code/CMakeModules/GetRevision.cmake create mode 100644 code/revision.h.in diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 4d429e693..bf5867327 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -91,6 +91,7 @@ NL_CONFIGURE_CHECKS() #Platform specifics SETUP_EXTERNAL() +NL_GEN_REVISION_H() IF(WIN32) SET(WINSOCK2_LIB ws2_32.lib) diff --git a/code/CMakeModules/FindMercurial.cmake b/code/CMakeModules/FindMercurial.cmake new file mode 100644 index 000000000..9c252ad17 --- /dev/null +++ b/code/CMakeModules/FindMercurial.cmake @@ -0,0 +1,108 @@ +# - Extract information from a subversion working copy +# The module defines the following variables: +# Mercurial_HG_EXECUTABLE - path to hg command line client +# Mercurial_VERSION_HG - version of hg command line client +# Mercurial_FOUND - true if the command line client was found +# MERCURIAL_FOUND - same as Mercurial_FOUND, set for compatiblity reasons +# +# The minimum required version of Mercurial can be specified using the +# standard syntax, e.g. FIND_PACKAGE(Mercurial 1.4) +# +# If the command line client executable is found two macros are defined: +# Mercurial_WC_INFO( ) +# Mercurial_WC_LOG( ) +# Mercurial_WC_INFO extracts information of a subversion working copy at +# a given location. This macro defines the following variables: +# _WC_URL - url of the repository (at ) +# _WC_ROOT - root url of the repository +# _WC_REVISION - current revision +# _WC_LAST_CHANGED_AUTHOR - author of last commit +# _WC_LAST_CHANGED_DATE - date of last commit +# _WC_LAST_CHANGED_REV - revision of last commit +# _WC_INFO - output of command `hg info ' +# Mercurial_WC_LOG retrieves the log message of the base revision of a +# subversion working copy at a given location. This macro defines the +# variable: +# _LAST_CHANGED_LOG - last log of base revision +# Example usage: +# FIND_PACKAGE(Mercurial) +# IF(MERCURIAL_FOUND) +# Mercurial_WC_INFO(${PROJECT_SOURCE_DIR} Project) +# MESSAGE("Current revision is ${Project_WC_REVISION}") +# Mercurial_WC_LOG(${PROJECT_SOURCE_DIR} Project) +# MESSAGE("Last changed log is ${Project_LAST_CHANGED_LOG}") +# ENDIF(MERCURIAL_FOUND) + +#============================================================================= +# Copyright 2006-2009 Kitware, Inc. +# Copyright 2006 Tristan Carel +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +FIND_PROGRAM(Mercurial_HG_EXECUTABLE hg + DOC "mercurial command line client") +MARK_AS_ADVANCED(Mercurial_HG_EXECUTABLE) + +IF(Mercurial_HG_EXECUTABLE) + EXECUTE_PROCESS(COMMAND ${Mercurial_HG_EXECUTABLE} --version + OUTPUT_VARIABLE Mercurial_VERSION_HG + OUTPUT_STRIP_TRAILING_WHITESPACE) + + STRING(REGEX REPLACE ".*version ([\\.0-9]+).*" + "\\1" Mercurial_VERSION_HG "${Mercurial_VERSION_HG}") + + MACRO(Mercurial_WC_INFO dir prefix) + EXECUTE_PROCESS(COMMAND ${Mercurial_HG_EXECUTABLE} tip + WORKING_DIRECTORY ${dir} + OUTPUT_VARIABLE ${prefix}_WC_INFO + ERROR_VARIABLE Mercurial_hg_info_error + RESULT_VARIABLE Mercurial_hg_info_result + OUTPUT_STRIP_TRAILING_WHITESPACE) + + IF(NOT ${Mercurial_hg_info_result} EQUAL 0) + MESSAGE(SEND_ERROR "Command \"${Mercurial_HG_EXECUTABLE} tip\" failed with output:\n${Mercurial_hg_info_error}") + ELSE(NOT ${Mercurial_hg_info_result} EQUAL 0) + + STRING(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*" + "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}") + STRING(REGEX REPLACE "^(.*\n)?changeset: *([0-9]+).*" + "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}") + STRING(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*" + "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}") + STRING(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*" + "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}") + STRING(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*" + "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}") + + ENDIF(NOT ${Mercurial_hg_info_result} EQUAL 0) + + ENDMACRO(Mercurial_WC_INFO) + + MACRO(Mercurial_WC_LOG dir prefix) + # This macro can block if the certificate is not signed: + # hg ask you to accept the certificate and wait for your answer + # This macro requires a hg server network access (Internet most of the time) + # and can also be slow since it access the hg server + EXECUTE_PROCESS(COMMAND + ${Mercurial_HG_EXECUTABLE} --non-interactive log -r BASE ${dir} + OUTPUT_VARIABLE ${prefix}_LAST_CHANGED_LOG + ERROR_VARIABLE Mercurial_hg_log_error + RESULT_VARIABLE Mercurial_hg_log_result + OUTPUT_STRIP_TRAILING_WHITESPACE) + + IF(NOT ${Mercurial_hg_log_result} EQUAL 0) + MESSAGE(SEND_ERROR "Command \"${Mercurial_HG_EXECUTABLE} log -r BASE ${dir}\" failed with output:\n${Mercurial_hg_log_error}") + ENDIF(NOT ${Mercurial_hg_log_result} EQUAL 0) + ENDMACRO(Mercurial_WC_LOG) +ENDIF(Mercurial_HG_EXECUTABLE) + +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Mercurial DEFAULT_MSG Mercurial_HG_EXECUTABLE) diff --git a/code/CMakeModules/GetRevision.cmake b/code/CMakeModules/GetRevision.cmake new file mode 100644 index 000000000..fdf32abef --- /dev/null +++ b/code/CMakeModules/GetRevision.cmake @@ -0,0 +1,44 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6.3) + +# ROOT_DIR should be set to root of the repository (where to find the .svn or .hg directory) +# SOURCE_DIR should be set to root of your code (where to find CMakeLists.txt) + +SET(CMAKE_MODULE_PATH "${SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}") + +MACRO(NOW RESULT) + IF (WIN32) + EXECUTE_PROCESS(COMMAND "wmic" "os" "get" "localdatetime" OUTPUT_VARIABLE DATETIME) + STRING(REGEX REPLACE ".*\n([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9]).*" "\\1-\\2-\\3 \\4:\\5:\\6" ${RESULT} ${DATETIME}) + ELSEIF(UNIX) + EXECUTE_PROCESS(COMMAND "date" "+'%Y-%m-%d %H:%M:%S'" OUTPUT_VARIABLE ${RESULT}) + ELSE (WIN32) + MESSAGE(SEND_ERROR "date not implemented") + SET(${RESULT} 000000) + ENDIF (WIN32) +ENDMACRO(NOW) + +IF(EXISTS "${ROOT_DIR}/.svn/") + FIND_PACKAGE(Subversion) + + IF(SUBVERSION_FOUND) + Subversion_WC_INFO(${ROOT_DIR} ER) + SET(REVISION ${ER_WC_REVISION}) + ENDIF(SUBVERSION_FOUND) +ENDIF(EXISTS "${ROOT_DIR}/.svn/") + +IF(EXISTS "${ROOT_DIR}/.hg/") + FIND_PACKAGE(Mercurial) + + IF(MERCURIAL_FOUND) + Mercurial_WC_INFO(${ROOT_DIR} ER) + SET(REVISION ${ER_WC_REVISION}) + ENDIF(MERCURIAL_FOUND) +ENDIF(EXISTS "${ROOT_DIR}/.hg/") + +IF(REVISION) + IF(EXISTS ${SOURCE_DIR}/revision.h.in) + NOW(BUILD_DATE) + CONFIGURE_FILE(${SOURCE_DIR}/revision.h.in revision.h.txt) + EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy revision.h.txt revision.h) # copy_if_different + ENDIF(EXISTS ${SOURCE_DIR}/revision.h.in) +ENDIF(REVISION) diff --git a/code/CMakeModules/nel.cmake b/code/CMakeModules/nel.cmake index 7dd43e8b4..e512e3787 100644 --- a/code/CMakeModules/nel.cmake +++ b/code/CMakeModules/nel.cmake @@ -9,6 +9,33 @@ MACRO(NL_GEN_PC name) ENDIF(NOT WIN32 AND WITH_INSTALL_LIBRARIES) ENDMACRO(NL_GEN_PC) +### +# Helper macro that generates revision.h from revision.h.in +### +MACRO(NL_GEN_REVISION_H) + IF(EXISTS ${CMAKE_SOURCE_DIR}/revision.h.in) + INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}) + ADD_DEFINITIONS(-DHAVE_REVISION_H) + SET(HAVE_REVISION_H ON) + + # a custom target that is always built + ADD_CUSTOM_TARGET(revision ALL + DEPENDS ${CMAKE_BINARY_DIR}/revision.h) + + # creates revision.h using cmake script + ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_BINARY_DIR}/revision.h + COMMAND ${CMAKE_COMMAND} + -DSOURCE_DIR=${CMAKE_SOURCE_DIR} + -DROOT_DIR=${CMAKE_SOURCE_DIR}/.. + -P ${CMAKE_SOURCE_DIR}/CMakeModules/GetRevision.cmake) + + # revision.h is a generated file + SET_SOURCE_FILES_PROPERTIES(${CMAKE_BINARY_DIR}/revision.h + PROPERTIES GENERATED TRUE + HEADER_FILE_ONLY TRUE) + ENDIF(EXISTS ${CMAKE_SOURCE_DIR}/revision.h.in) +ENDMACRO(NL_GEN_REVISION_H) + ### # ### diff --git a/code/revision.h.in b/code/revision.h.in new file mode 100644 index 000000000..6c5e9b8b1 --- /dev/null +++ b/code/revision.h.in @@ -0,0 +1,7 @@ +#ifndef REVISION_H +#define REVISION_H + +#cmakedefine REVISION "${REVISION}" +#cmakedefine BUILD_DATE "${BUILD_DATE}" + +#endif