Merge lp:~michihenning/unity-api/static-test-lib into lp:unity-api

Proposed by Michi Henning
Status: Merged
Approved by: Jussi Pakkanen
Approved revision: 62
Merged at revision: 62
Proposed branch: lp:~michihenning/unity-api/static-test-lib
Merge into: lp:unity-api
Diff against target: 138 lines (+34/-9)
8 files modified
CMakeLists.txt (+7/-0)
src/CMakeLists.txt (+20/-2)
test/gtest/CMakeLists.txt (+2/-2)
test/gtest/unity/CMakeLists.txt (+1/-1)
test/gtest/unity/util/Daemon/CMakeLists.txt (+1/-1)
test/gtest/unity/util/DefinesPtrs/CMakeLists.txt (+1/-1)
test/gtest/unity/util/FileIO/CMakeLists.txt (+1/-1)
test/gtest/unity/util/ResourcePtr/CMakeLists.txt (+1/-1)
To merge this branch: bzr merge lp:~michihenning/unity-api/static-test-lib
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Jussi Pakkanen (community) Needs Fixing
Review via email: mp+169984@code.launchpad.net

Commit message

This changes the build environment to link the tests against a static version of the library, allows us to write unit tests for internal classes that are not visible through the public API (support classes in the internal namespace that don't have a public pimple facade).

Code is always compiled with -fPIC, so it can be used for both static and dynamic versions and we don't need to compile every source file twice with different compiler flags.

Description of the change

This changes the build environment to link the tests against a static version of the library, allows us to write unit tests for internal classes that are not visible through the public API (support classes in the internal namespace that don't have a public pimple facade).

Code is always compiled with -fPIC, so it can be used for both static and dynamic versions and we don't need to compile every source file twice with different compiler flags.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Jussi Pakkanen (jpakkane) wrote :

This is the kind of code that makes you want to take long showers to get the dirt off but I can't really think of a cleaner way to do it.

One potential change is that instead of using a global argument:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

you should consider using a per-target compile flag to the library (i.e. with set_properties).

review: Needs Fixing
Revision history for this message
Michi Henning (michihenning) wrote :

> This is the kind of code that makes you want to take long showers to get the
> dirt off but I can't really think of a cleaner way to do it.

Not entirely sure what you mean here :-) I can't think of a cleaner way either. Two libraries seem to be only viable option.

> One potential change is that instead of using a global argument:
>
> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
>
> you should consider using a per-target compile flag to the library (i.e. with
> set_properties).

You mean using set_properties on the static version? (The dynamic one is build with -fPIC anyway.) Would make any real difference? Or, alternatively, is there a problem with setting -fPIC for all sources?

Revision history for this message
Jussi Pakkanen (jpakkane) wrote :

Putting -fPIC to global flags is bad for all the reasons global state and global variables are bad. There are also potential clashes. For example, Qt5 executables must be build with -fPIE. What happens if you define both? I don't know, nor do I really want to spend time finding out.

FPic is needed because of one library. It is the property of said library. Therefore expressing it specifically as such is the right thing to do.

Revision history for this message
Michi Henning (michihenning) wrote :

> FPic is needed because of one library. It is the property of said library.
> Therefore expressing it specifically as such is the right thing to do.

Fair enough, that makes sense.

62. By Michi Henning

Changed build to use -fPIC only for the source files that go into the .so and .a libraries. Improved comments and fixed some typos in comments.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-06-17 08:02:36 +0000
3+++ CMakeLists.txt 2013-06-18 22:51:27 +0000
4@@ -117,12 +117,19 @@
5 # API library
6 set(UNITY_API_LIB unity-api)
7
8+# Static version for testing
9+set(UNITY_API_STATIC_LIB unity-api-static)
10+
11 # Other libraries we depend on
12 set(OTHER_API_LIBS )
13
14 # All the libraries we need to link a normal executable that uses the Unity API
15 set(LIBS ${UNITY_API_LIB} ${OTHER_API_LIBS})
16
17+# All the libraries we need to link a gtest executable. (We link the tests against a static version
18+# so we can do whitebox testing on internal classes.
19+set(TESTLIBS ${UNITY_API_STATIC_LIB} ${OTHER_API_LIBS})
20+
21 # Library install prefix
22 set(LIB_INSTALL_PREFIX lib/${CMAKE_LIBRARY_ARCHITECTURE})
23
24
25=== modified file 'src/CMakeLists.txt'
26--- src/CMakeLists.txt 2013-05-17 00:28:40 +0000
27+++ src/CMakeLists.txt 2013-06-18 22:51:27 +0000
28@@ -1,16 +1,34 @@
29 add_subdirectory(unity)
30
31+# Pseudo-library of object files. We need a dynamic version of the library for normal clients,
32+# and a static version for the whitebox tests, so we can write unit tests for classes in the internal namespaces
33+# (because, for the .so, non-public APIs are compiled with -fvisibility=hidden).
34+# Everything is compiled with -fPIC, so the same object files are suitable for either library.
35+# Here, we create an object library that then is used to link the static and dynamic
36+# libraries, without having to compile each source file twice with different compile flags.
37+
38+set(UNITY_API_LIB_OBJ ${UNITY_API_LIB}-obj)
39+add_library(${UNITY_API_LIB_OBJ} OBJECT ${UNITY_API_LIB_SRC})
40+
41+# Use the object files to make the shared library.
42 set(UNITY_API_SOVERSION 0)
43-
44-add_library(${UNITY_API_LIB} SHARED ${UNITY_API_LIB_SRC})
45+add_library(${UNITY_API_LIB} SHARED $<TARGET_OBJECTS:${UNITY_API_LIB_OBJ}>)
46 set_target_properties(${UNITY_API_LIB} PROPERTIES
47 VERSION "${UNITY_API_MAJOR}.${UNITY_API_MINOR}"
48 SOVERSION ${UNITY_API_SOVERSION}
49 )
50
51+# Use the object files to make the static library. We add -fPIC to avoid compiling a second time.
52+set_source_files_properties(${UNITY_API_LIB_SRC} PROPERTIES COMPILE_FLAGS "-fPIC")
53+add_library(${UNITY_API_STATIC_LIB} STATIC $<TARGET_OBJECTS:${UNITY_API_LIB_OBJ}>)
54+set_target_properties(${UNITY_API_STATIC_LIB} PROPERTIES OUTPUT_NAME ${UNITY_API_LIB})
55+
56+# Only the dynamic library gets installed.
57 install(TARGETS ${UNITY_API_LIB} LIBRARY DESTINATION ${LIB_INSTALL_PREFIX})
58
59+# Set up package config.
60 configure_file(lib${UNITY_API_LIB}.pc.in lib${UNITY_API_LIB}.pc @ONLY)
61 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${UNITY_API_LIB}.pc DESTINATION ${LIB_INSTALL_PREFIX}/pkgconfig)
62
63+# Parent needs to know what all the source files are, for generating doc and the like.
64 set(UNITY_API_LIB_SRC ${UNITY_API_LIB_SRC} ${UNITY_SRC} PARENT_SCOPE)
65
66=== modified file 'test/gtest/CMakeLists.txt'
67--- test/gtest/CMakeLists.txt 2013-05-13 11:39:49 +0000
68+++ test/gtest/CMakeLists.txt 2013-06-18 22:51:27 +0000
69@@ -1,7 +1,7 @@
70 find_package(Threads REQUIRED)
71 set(TESTLIBDIR ${CMAKE_BINARY_DIR}/test/gtest/libgtest/build)
72 set(LIBGTEST gtest)
73-set(TESTLIBS ${LIBGTEST} ${CMAKE_THREAD_LIBS_INIT})
74+set(TESTLIBS ${TESTLIBS} ${LIBGTEST} ${CMAKE_THREAD_LIBS_INIT})
75
76 add_subdirectory(libgtest)
77 add_subdirectory(unity)
78@@ -12,7 +12,7 @@
79 foreach(src ${TEST_SRC})
80 get_filename_component(name ${src} NAME_WE)
81 add_executable(${name} ${src})
82- target_link_libraries(${name} ${LIBS} ${TESTLIBS})
83+ target_link_libraries(${name} ${TESTLIBS})
84 string(REPLACE "_test" "" test_name ${name})
85 add_test(${test_name} ${name})
86 endforeach(src)
87
88=== modified file 'test/gtest/unity/CMakeLists.txt'
89--- test/gtest/unity/CMakeLists.txt 2013-05-13 11:39:49 +0000
90+++ test/gtest/unity/CMakeLists.txt 2013-06-18 22:51:27 +0000
91@@ -3,6 +3,6 @@
92 add_subdirectory(util)
93
94 add_executable(Exceptions_test Exceptions_test.cpp)
95-target_link_libraries(Exceptions_test ${LIBS} ${TESTLIBS})
96+target_link_libraries(Exceptions_test ${TESTLIBS})
97
98 add_test(Exceptions Exceptions_test)
99
100=== modified file 'test/gtest/unity/util/Daemon/CMakeLists.txt'
101--- test/gtest/unity/util/Daemon/CMakeLists.txt 2013-05-13 11:39:49 +0000
102+++ test/gtest/unity/util/Daemon/CMakeLists.txt 2013-06-18 22:51:27 +0000
103@@ -1,4 +1,4 @@
104 add_executable(Daemon_test Daemon_test.cpp)
105-target_link_libraries(Daemon_test ${LIBS} ${TESTLIBS})
106+target_link_libraries(Daemon_test ${TESTLIBS})
107
108 add_test(Daemon ${CMAKE_CURRENT_SOURCE_DIR}/daemon-tester.py ${CMAKE_CURRENT_BINARY_DIR}/Daemon_test)
109
110=== modified file 'test/gtest/unity/util/DefinesPtrs/CMakeLists.txt'
111--- test/gtest/unity/util/DefinesPtrs/CMakeLists.txt 2013-05-13 11:39:49 +0000
112+++ test/gtest/unity/util/DefinesPtrs/CMakeLists.txt 2013-06-18 22:51:27 +0000
113@@ -1,4 +1,4 @@
114 add_executable(DefinesPtrs_test DefinesPtrs_test.cpp)
115-target_link_libraries(DefinesPtrs_test ${LIBS} ${TESTLIBS})
116+target_link_libraries(DefinesPtrs_test ${TESTLIBS})
117
118 add_test(DefinesPtrs DefinesPtrs_test)
119
120=== modified file 'test/gtest/unity/util/FileIO/CMakeLists.txt'
121--- test/gtest/unity/util/FileIO/CMakeLists.txt 2013-05-13 11:39:49 +0000
122+++ test/gtest/unity/util/FileIO/CMakeLists.txt 2013-06-18 22:51:27 +0000
123@@ -1,4 +1,4 @@
124 add_executable(FileIO_test FileIO_test.cpp)
125-target_link_libraries(FileIO_test ${LIBS} ${TESTLIBS})
126+target_link_libraries(FileIO_test ${TESTLIBS})
127
128 add_test(FileIO FileIO_test)
129
130=== modified file 'test/gtest/unity/util/ResourcePtr/CMakeLists.txt'
131--- test/gtest/unity/util/ResourcePtr/CMakeLists.txt 2013-05-29 04:45:19 +0000
132+++ test/gtest/unity/util/ResourcePtr/CMakeLists.txt 2013-06-18 22:51:27 +0000
133@@ -1,4 +1,4 @@
134 add_executable(ResourcePtr_test ResourcePtr_test.cpp)
135-target_link_libraries(ResourcePtr_test ${LIBS} ${TESTLIBS})
136+target_link_libraries(ResourcePtr_test ${TESTLIBS})
137
138 add_test(ResourcePtr ResourcePtr_test)

Subscribers

People subscribed via source and target branches

to all changes: