Skip to content

Commit 970678f

Browse files
committed
cmake: group files in the IDE automatically
1 parent 67a0d96 commit 970678f

File tree

5 files changed

+49
-54
lines changed

5 files changed

+49
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file(see http://k
88
- map: write value to variable only when it exist.
99
- ini: user can retrieve error position when syntax error.
1010
- json: user can retrieve error position when syntax error.
11+
- cmake: group files in the IDE automatically.
1112

1213
### Changed
1314
- fs: accept two types of lambda callbacks.

CMakeLists.txt

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,21 @@ cmake_minimum_required(VERSION 2.8)
77
project(libchen)
88

99
# environment
10-
include(cmake/Environment.cmake)
10+
include(cmake/ChenInit.cmake)
11+
include(cmake/ChenUtil.cmake)
1112

1213
# include path
1314
include_directories(include)
1415

1516
# source codes
16-
file(GLOB INC_ROOT include/chen/*.hpp)
17-
file(GLOB INC_BASE include/chen/base/*.hpp)
18-
file(GLOB INC_DATA include/chen/data/*.hpp)
19-
file(GLOB INC_MT include/chen/mt/*.hpp)
20-
file(GLOB INC_SYS include/chen/sys/*.hpp)
21-
file(GLOB INC_TIME include/chen/time/*.hpp)
22-
file(GLOB INC_TOOL include/chen/tool/*.hpp)
17+
file(GLOB_RECURSE INC_CHEN include/chen/*.hpp)
18+
file(GLOB_RECURSE SRC_CHEN src/*.cpp)
2319

24-
aux_source_directory(src/base SRC_BASE)
25-
aux_source_directory(src/data SRC_DATA)
26-
aux_source_directory(src/mt SRC_MT)
27-
aux_source_directory(src/sys SRC_SYS)
28-
aux_source_directory(src/time SRC_TIME)
29-
aux_source_directory(src/tool SRC_TOOL)
30-
31-
# generate lib, default is a static library
20+
# generate lib, build static library by default
3221
# use -DBUILD_SHARED_LIBS=ON if you want to build a shared library
33-
set(ALL_FILES
34-
${INC_BASE} ${INC_DATA} ${INC_MT} ${INC_SYS} ${INC_TIME} ${INC_TOOL} ${INC_ROOT}
35-
${SRC_BASE} ${SRC_DATA} ${SRC_MT} ${SRC_SYS} ${SRC_TIME} ${SRC_TOOL})
36-
37-
add_library(libchen ${ALL_FILES})
22+
add_library(libchen ${INC_CHEN} ${SRC_CHEN})
3823
set_target_properties(libchen PROPERTIES OUTPUT_NAME libchen PREFIX "")
3924

40-
# group files in IDEs like Xcode and Visual Studio
41-
source_group("include\\chen" FILES ${INC_ROOT})
42-
source_group("include\\chen\\base" FILES ${INC_BASE})
43-
source_group("include\\chen\\data" FILES ${INC_DATA})
44-
source_group("include\\chen\\mt" FILES ${INC_MT})
45-
source_group("include\\chen\\sys" FILES ${INC_SYS})
46-
source_group("include\\chen\\time" FILES ${INC_TIME})
47-
source_group("include\\chen\\tool" FILES ${INC_TOOL})
48-
49-
source_group("src\\base" FILES ${SRC_BASE})
50-
source_group("src\\data" FILES ${SRC_DATA})
51-
source_group("src\\mt" FILES ${SRC_MT})
52-
source_group("src\\sys" FILES ${SRC_SYS})
53-
source_group("src\\time" FILES ${SRC_TIME})
54-
source_group("src\\tool" FILES ${SRC_TOOL})
25+
# group files in the IDE(e.g: Xcode and Visual Studio)
26+
chen_group_files(${CMAKE_CURRENT_SOURCE_DIR} "${INC_CHEN}")
27+
chen_group_files(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_CHEN}")

cmake/Environment.cmake renamed to cmake/ChenInit.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# This file contains some useful macros and functions
21
# Jian Chen <admin@chensoft.com>
32
# http://chensoft.com
43
# Licensed under MIT license

cmake/ChenUtil.cmake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Jian Chen <admin@chensoft.com>
2+
# http://chensoft.com
3+
# Licensed under MIT license
4+
# Copyright 2016 Jian Chen
5+
6+
# group files in the IDE(e.g: Xcode and Visual Studio)
7+
function(chen_group_files prefix list)
8+
# check prefix length, include the trailing '/'
9+
string(LENGTH ${prefix} length)
10+
math(EXPR length "${length} + 1")
11+
12+
foreach(name ${list})
13+
# retrieve the dirname
14+
get_filename_component(folder ${name} PATH)
15+
string(LENGTH ${folder} count)
16+
17+
if(NOT (${length} GREATER ${count}))
18+
string(SUBSTRING ${folder} ${length} -1 group)
19+
string(REPLACE "/" "\\\\" group ${group})
20+
endif()
21+
22+
# add files into group
23+
if(NOT group)
24+
source_group("" FILES ${name})
25+
else()
26+
source_group(${group} FILES ${name})
27+
endif()
28+
endforeach()
29+
endfunction()

test/CMakeLists.txt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ cmake_minimum_required(VERSION 2.8)
77
project(test_libchen)
88

99
# environment
10-
include(../cmake/Environment.cmake)
10+
include(../cmake/ChenInit.cmake)
11+
include(../cmake/ChenUtil.cmake)
1112

1213
if(UNIX)
1314
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
@@ -33,26 +34,18 @@ include_directories(lib/googletest/googletest/include)
3334

3435
# source codes
3536
file(GLOB INC_ROOT *.hpp)
37+
file(GLOB SRC_ROOT *.CPP)
38+
file(GLOB_RECURSE SRC_TEST src/*.cpp)
3639

37-
aux_source_directory(. SRC_ROOT)
38-
aux_source_directory(src/base SRC_BASE)
39-
aux_source_directory(src/data SRC_DATA)
40-
aux_source_directory(src/mt SRC_MT)
41-
aux_source_directory(src/sys SRC_SYS)
42-
aux_source_directory(src/tool SRC_TOOL)
40+
set(INC_TEST ${INC_ROOT})
41+
set(SRC_TEST ${SRC_ROOT} ${SRC_TEST})
4342

4443
# generate app
45-
add_executable(test_libchen ${INC_ROOT} ${SRC_ROOT} ${SRC_BASE} ${SRC_DATA} ${SRC_MT} ${SRC_SYS} ${SRC_TOOL})
44+
add_executable(test_libchen ${INC_TEST} ${SRC_TEST})
4645

4746
# link library
4847
target_link_libraries(test_libchen libchen gtest)
4948

50-
# group files in IDEs like Xcode and Visual Studio
51-
source_group("" FILES ${INC_ROOT})
52-
source_group("" FILES ${SRC_ROOT})
53-
54-
source_group("src\\base" FILES ${SRC_BASE})
55-
source_group("src\\data" FILES ${SRC_DATA})
56-
source_group("src\\mt" FILES ${SRC_MT})
57-
source_group("src\\sys" FILES ${SRC_SYS})
58-
source_group("src\\tool" FILES ${SRC_TOOL})
49+
# group files in the IDE(e.g: Xcode and Visual Studio)
50+
chen_group_files(${CMAKE_CURRENT_SOURCE_DIR} "${INC_TEST}")
51+
chen_group_files(${CMAKE_CURRENT_SOURCE_DIR} "${SRC_TEST}")

0 commit comments

Comments
 (0)