Skip to content

Commit

Permalink
Merge pull request #19 from yajiedesign/addcmake
Browse files Browse the repository at this point in the history
add cmake
  • Loading branch information
mli committed Oct 11, 2015
2 parents 43385e2 + 1821a89 commit 504faa7
Show file tree
Hide file tree
Showing 12 changed files with 1,497 additions and 4 deletions.
40 changes: 40 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 2.8.7)

project(pslite C CXX)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)

include(ExternalProject)
set(pslite_LINKER_LIBS "")

# ---[ Google-gflags
include("cmake/External/gflags.cmake")
include_directories(pslite ${GFLAGS_INCLUDE_DIRS})
list(APPEND pslite_LINKER_LIBS ${GFLAGS_LIBRARIES})

# ---[ Google-glog
include("cmake/External/glog.cmake")
include_directories(pslite ${GLOG_INCLUDE_DIRS})
list(APPEND pslite_LINKER_LIBS ${GLOG_LIBRARIES})

# ---[ Google-protobuf
include(cmake/ProtoBuf.cmake)

# generate protobuf sources
set(proto_gen_folder "${PROJECT_BINARY_DIR}/include")
file(GLOB proto_files "src/proto/*.proto")
pslite_protobuf_generate_cpp_py(${proto_gen_folder} proto_srcs proto_hdrs proto_python "${PROJECT_SOURCE_DIR}" "src" ${proto_files})
include_directories(pslite "${PROJECT_BINARY_DIR}/include/")

FILE(GLOB SOURCE "src/*.cc")

if(MSVC)
FILE(GLOB getopt_SOURCE "src/windows/getopt.c")
list(APPEND SOURCE ${getopt_SOURCE})
add_definitions(-DSTATIC_GETOPT)
endif()

list(APPEND SOURCE ${proto_srcs})
add_library(pslite ${SOURCE})

target_link_libraries(pslite ${pslite_LINKER_LIBS})
10 changes: 10 additions & 0 deletions cmake/External/getopt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (NOT __GETOPT_INCLUDED) # guard against multiple includes
set(__GETOPT_INCLUDED TRUE)
include(CheckIncludeFile)
check_include_file("getopt.h" HAVE_GETOPT_H)
if(NOT HAVE_GETOPT_H)
find_path(GETOPT_INCLUDE_DIRS "getopt.h")
find_library(GETOPT_LIBRARIES "getopt")
endif()

endif()
60 changes: 60 additions & 0 deletions cmake/External/gflags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
if (NOT __GFLAGS_INCLUDED) # guard against multiple includes
set(__GFLAGS_INCLUDED TRUE)

# use the system-wide gflags if present
find_package(GFlags)
if (GFLAGS_FOUND)
set(GFLAGS_EXTERNAL FALSE)
else()
# gflags will use pthreads if it's available in the system, so we must link with it
find_package(Threads)

# build directory
set(gflags_PREFIX ${CMAKE_BINARY_DIR}/external/gflags-prefix)
# install directory
set(gflags_INSTALL ${CMAKE_BINARY_DIR}/external/gflags-install)

# we build gflags statically, but want to link it into the caffe shared library
# this requires position-independent code
if (UNIX)
set(GFLAGS_EXTRA_COMPILER_FLAGS "-fPIC")
endif()

set(GFLAGS_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${GFLAGS_EXTRA_COMPILER_FLAGS})
set(GFLAGS_C_FLAGS ${CMAKE_C_FLAGS} ${GFLAGS_EXTRA_COMPILER_FLAGS})

ExternalProject_Add(gflags
PREFIX ${gflags_PREFIX}
GIT_REPOSITORY "https://github.com/gflags/gflags.git"
GIT_TAG "v2.1.2"
UPDATE_COMMAND ""
INSTALL_DIR ${gflags_INSTALL}
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX=${gflags_INSTALL}
-DBUILD_SHARED_LIBS=OFF
-DBUILD_STATIC_LIBS=ON
-DBUILD_PACKAGING=OFF
-DBUILD_TESTING=OFF
-DBUILD_NC_TESTS=OFF
-BUILD_CONFIG_TESTS=OFF
-DINSTALL_HEADERS=ON
-DCMAKE_C_FLAGS=${GFLAGS_C_FLAGS}
-DCMAKE_CXX_FLAGS=${GFLAGS_CXX_FLAGS}
LOG_DOWNLOAD 1
LOG_INSTALL 1
)

set(GFLAGS_FOUND TRUE)
set(GFLAGS_INCLUDE_DIRS ${gflags_INSTALL}/include)
if(MSVC)
set(GFLAGS_LIBRARIES ${gflags_INSTALL}/lib/gflags.lib ${CMAKE_THREAD_LIBS_INIT})
else()
set(GFLAGS_LIBRARIES ${gflags_INSTALL}/lib/libgflags.a ${CMAKE_THREAD_LIBS_INIT})
endif()
set(GFLAGS_LIBRARY_DIRS ${gflags_INSTALL}/lib)
set(GFLAGS_EXTERNAL TRUE)

list(APPEND external_project_dependencies gflags)
endif()

endif()
71 changes: 71 additions & 0 deletions cmake/External/glog.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# glog depends on gflags
include("cmake/External/gflags.cmake")

set(GFLAGS_ROOT_DIR ${gflags_INSTALL})

if (NOT __GLOG_INCLUDED)
set(__GLOG_INCLUDED TRUE)

# try the system-wide glog first
find_package(Glog)
if (GLOG_FOUND)
set(GLOG_EXTERNAL FALSE)
else()
# fetch and build glog from github

# build directory
set(glog_PREFIX ${CMAKE_BINARY_DIR}/external/glog-prefix)
# install directory
set(glog_INSTALL ${CMAKE_BINARY_DIR}/external/glog-install)

# we build glog statically, but want to link it into the caffe shared library
# this requires position-independent code
if (UNIX)
set(GLOG_EXTRA_COMPILER_FLAGS "-fPIC")
endif()

set(GLOG_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${GLOG_EXTRA_COMPILER_FLAGS})
set(GLOG_C_FLAGS ${CMAKE_C_FLAGS} ${GLOG_EXTRA_COMPILER_FLAGS})

# depend on gflags if we're also building it
if (GFLAGS_EXTERNAL)
set(GLOG_DEPENDS gflags)
endif()


ExternalProject_Add(glog
DEPENDS ${GLOG_DEPENDS}
PREFIX ${glog_PREFIX}
GIT_REPOSITORY "https://github.com/google/glog"
UPDATE_COMMAND ""
INSTALL_DIR ${glog_INSTALL}
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX=${glog_INSTALL}
-DCMAKE_PREFIX_PATH=${gflags_INSTALL}
-Dgflags_FOUND=ON
-Dgflags_INCLUDE_DIR=${gflags_INSTALL}/Include
-Dgflags_INCLUDE_DIR=${gflags_INSTALL}/Lib/gflags
-DBUILD_SHARED_LIBS=OFF
-DINSTALL_HEADERS=ON
-DCMAKE_C_FLAGS=${GLOG_C_FLAGS}
-DCMAKE_CXX_FLAGS=${GLOG_CXX_FLAGS}
LOG_DOWNLOAD 1
LOG_CONFIGURE 1
LOG_INSTALL 1
)

set(GLOG_FOUND TRUE)
set(GLOG_INCLUDE_DIRS ${glog_INSTALL}/include)
if(MSVC)
set(GLOG_LIBRARIES ${GFLAGS_LIBRARIES} ${glog_INSTALL}/lib/glog.lib)
else()
set(GLOG_LIBRARIES ${GFLAGS_LIBRARIES} ${glog_INSTALL}/lib/libglog.a)
endif()
set(GLOG_LIBRARY_DIRS ${glog_INSTALL}/lib)
set(GLOG_EXTERNAL TRUE)

list(APPEND external_project_dependencies glog)
endif()

endif()

48 changes: 48 additions & 0 deletions cmake/Modules/FindGlog.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# - Try to find Glog
#
# The following variables are optionally searched for defaults
# GLOG_ROOT_DIR: Base directory where all GLOG components are found
#
# The following are set after configuration is done:
# GLOG_FOUND
# GLOG_INCLUDE_DIRS
# GLOG_LIBRARIES
# GLOG_LIBRARYRARY_DIRS

include(FindPackageHandleStandardArgs)

set(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog")

if(WIN32)
find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_ROOT_DIR}/src/windows)
else()
find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_ROOT_DIR})
endif()

if(MSVC)
find_library(GLOG_LIBRARY_RELEASE libglog_static
PATHS ${GLOG_ROOT_DIR}
PATH_SUFFIXES Release)

find_library(GLOG_LIBRARY_DEBUG libglog_static
PATHS ${GLOG_ROOT_DIR}
PATH_SUFFIXES Debug)

set(GLOG_LIBRARY optimized ${GLOG_LIBRARY_RELEASE} debug ${GLOG_LIBRARY_DEBUG})
else()
find_library(GLOG_LIBRARY glog
PATHS ${GLOG_ROOT_DIR}
PATH_SUFFIXES lib lib64)
endif()

find_package_handle_standard_args(Glog DEFAULT_MSG GLOG_INCLUDE_DIR GLOG_LIBRARY)

if(GLOG_FOUND)
set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
set(GLOG_LIBRARIES ${GLOG_LIBRARY})
message(STATUS "Found glog (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})")
mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUG
GLOG_LIBRARY GLOG_INCLUDE_DIR)
endif()
85 changes: 85 additions & 0 deletions cmake/ProtoBuf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Finds Google Protocol Buffers library and compilers and extends
# the standard cmake script with version and python generation support

find_package( Protobuf REQUIRED )
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIR})
list(APPEND pslite_LINKER_LIBS ${PROTOBUF_LIBRARIES})

# As of Ubuntu 14.04 protoc is no longer a part of libprotobuf-dev package
# and should be installed separately as in: sudo apt-get install protobuf-compiler
if(EXISTS ${PROTOBUF_PROTOC_EXECUTABLE})
message(STATUS "Found PROTOBUF Compiler: ${PROTOBUF_PROTOC_EXECUTABLE}")
else()
message(FATAL_ERROR "Could not find PROTOBUF Compiler")
endif()


# place where to generate protobuf sources
set(proto_gen_folder "${PROJECT_BINARY_DIR}/include/pslite/proto")
include_directories(SYSTEM "${PROJECT_BINARY_DIR}/include")

set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)

################################################################################################
# Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
# Usage:
# pslite_protobuf_generate_cpp_py(<output_dir> <srcs_var> <hdrs_var> <python_var> <proto_files>)
function(pslite_protobuf_generate_cpp_py output_dir srcs_var hdrs_var python_var work_path proto_path)
if(NOT ARGN)
message(SEND_ERROR "Error: pslite_protobuf_generate_cpp_py() called without any proto files")
return()
endif()

if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
# Create an include path for each file specified
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(abs_path ${abs_fil} PATH)
list(FIND _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protoc_include -I ${abs_path})
endif()
endforeach()
else()
set(_protoc_include -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(dir ${PROTOBUF_IMPORT_DIRS})
get_filename_component(abs_path ${dir} ABSOLUTE)
list(FIND _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protoc_include -I ${abs_path})
endif()
endforeach()
endif()

set(${srcs_var})
set(${hdrs_var})
set(${python_var})
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(fil_we ${fil} NAME_WE)
string(REPLACE ${work_path}/ "" o_fil ${abs_fil})

list(APPEND ${srcs_var} "${output_dir}/proto/${fil_we}.pb.cc")
list(APPEND ${hdrs_var} "${output_dir}/proto/${fil_we}.pb.h")
list(APPEND ${python_var} "${output_dir}/proto/${fil_we}_pb2.py")

add_custom_command(
OUTPUT "${output_dir}/proto/${fil_we}.pb.cc"
"${output_dir}/proto/${fil_we}.pb.h"
"${output_dir}/proto/${fil_we}_pb2.py"
COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out ${output_dir} ${_protoc_include} ${o_fil} --proto_path src
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${output_dir} ${_protoc_include} ${o_fil} --proto_path src
DEPENDS ${abs_fil}
WORKING_DIRECTORY ${work_path}
COMMENT "Running C++/Python protocol buffer compiler on ${o_fil}" VERBATIM )
endforeach()

set_source_files_properties(${${srcs_var}} ${${hdrs_var}} ${${python_var}} PROPERTIES GENERATED TRUE)
set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
set(${hdrs_var} ${${hdrs_var}} PARENT_SCOPE)
set(${python_var} ${${python_var}} PARENT_SCOPE)
endfunction()
10 changes: 10 additions & 0 deletions src/base/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include "../windows/unistd.h"
#else
#include <unistd.h>
#endif
#include <time.h>
#ifdef _MSC_VER
#include <sys/timeb.h>
#include <sys/types.h>
#else
#include <sys/time.h>
#include <libgen.h>
#endif


// concurrency
#include <future>
Expand Down
4 changes: 2 additions & 2 deletions src/base/resource_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static double milliToc(system_clock::time_point start) {
system_clock::now() - start).count();
return static_cast<double>(ct);
}

#ifndef _MSC_VER
inline struct timespec hwtic() {
struct timespec tv;
#ifdef __MACH__
Expand All @@ -57,7 +57,7 @@ inline double hwtoc(struct timespec tv) {
return (double) ((curr.tv_sec - tv.tv_sec) +
(curr.tv_nsec -tv.tv_nsec)*1e-9);
}

#endif
class ScopedTimer {
public:
explicit ScopedTimer(double* aggregate_time) :
Expand Down
19 changes: 17 additions & 2 deletions src/ps/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,22 @@ inline std::string DebugStr(const V* data, int n, int m = 5) {
}
return ss.str();
}

#ifdef _MSC_VER
/*! \brief printf-style logging */
#define NOTICE(_fmt_, args...) do { \
#define NOTICE(_fmt_,...) do { \
struct timeval tv; gettimeofday(&tv, NULL); \
time_t ts = (time_t)(tv.tv_sec); \
struct ::tm tm_time; localtime_r(&ts, &tm_time); \
int n = strlen(__FILE__) - 1; \
for (; n > -1; --n) { if (n==-1 || __FILE__[n] == '/') break; } \
fprintf(stdout, "[%02d%02d %02d:%02d:%02d.%03d %s:%d] " _fmt_ "\n", \
1+tm_time.tm_mon, tm_time.tm_mday, tm_time.tm_hour, \
tm_time.tm_min, tm_time.tm_sec, (int)tv.tv_usec/1000, \
__FILE__+n+1, __LINE__, __VA_ARGS__); \
} while (0)
#else
/*! \brief printf-style logging */
#define NOTICE(_fmt_, args...) do { \
struct timeval tv; gettimeofday(&tv, NULL); \
time_t ts = (time_t)(tv.tv_sec); \
struct ::tm tm_time; localtime_r(&ts, &tm_time); \
Expand All @@ -72,6 +85,8 @@ inline std::string DebugStr(const V* data, int n, int m = 5) {
tm_time.tm_min, tm_time.tm_sec, (int)tv.tv_usec/1000, \
__FILE__+n+1, __LINE__, ##args); \
} while (0)
#endif


#ifndef DISALLOW_COPY_AND_ASSIGN
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
Expand Down
Loading

0 comments on commit 504faa7

Please sign in to comment.