From 69d3d1835c5f4bdf9fbe5e920517a74d82482455 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 12 Mar 2012 14:41:34 -0400 Subject: Test OBJECT library success cases Add "ObjectLibrary" test to build and use OBJECT libraries. Build multiple object libraries in separate directories with different flags. Use a custom command to generate a source file in one OBJECT library. Reference the OBJECT libraries for inclusion in a STATIC library, a SHARED library, and an EXECUTABLE target. Use the static and shared libraries each in executables that end up using the object library symbols. Verify that object library symbols are exported from the shared library. --- Tests/CMakeLists.txt | 1 + Tests/ObjectLibrary/A/CMakeLists.txt | 17 +++++++++++++++++ Tests/ObjectLibrary/A/a.h | 6 ++++++ Tests/ObjectLibrary/A/a1.c.in | 2 ++ Tests/ObjectLibrary/A/a2.c | 2 ++ Tests/ObjectLibrary/B/CMakeLists.txt | 15 +++++++++++++++ Tests/ObjectLibrary/B/b.h | 11 +++++++++++ Tests/ObjectLibrary/B/b1.c | 2 ++ Tests/ObjectLibrary/B/b1_vs6.c | 1 + Tests/ObjectLibrary/B/b2.c | 2 ++ Tests/ObjectLibrary/B/b2_vs6.c | 1 + Tests/ObjectLibrary/CMakeLists.txt | 16 ++++++++++++++++ Tests/ObjectLibrary/c.c | 19 +++++++++++++++++++ Tests/ObjectLibrary/main.c | 16 ++++++++++++++++ 14 files changed, 111 insertions(+) create mode 100644 Tests/ObjectLibrary/A/CMakeLists.txt create mode 100644 Tests/ObjectLibrary/A/a.h create mode 100644 Tests/ObjectLibrary/A/a1.c.in create mode 100644 Tests/ObjectLibrary/A/a2.c create mode 100644 Tests/ObjectLibrary/B/CMakeLists.txt create mode 100644 Tests/ObjectLibrary/B/b.h create mode 100644 Tests/ObjectLibrary/B/b1.c create mode 100644 Tests/ObjectLibrary/B/b1_vs6.c create mode 100644 Tests/ObjectLibrary/B/b2.c create mode 100644 Tests/ObjectLibrary/B/b2_vs6.c create mode 100644 Tests/ObjectLibrary/CMakeLists.txt create mode 100644 Tests/ObjectLibrary/c.c create mode 100644 Tests/ObjectLibrary/main.c diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index cf4dc44..7baed2c 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -199,6 +199,7 @@ IF(BUILD_TESTING) ADD_TEST_MACRO(CxxOnly CxxOnly) ADD_TEST_MACRO(IPO COnly/COnly) ADD_TEST_MACRO(OutDir runtime/OutDir) + ADD_TEST_MACRO(ObjectLibrary UseCshared) ADD_TEST_MACRO(NewlineArgs NewlineArgs) ADD_TEST_MACRO(SetLang SetLang) ADD_TEST_MACRO(ExternalOBJ ExternalOBJ) diff --git a/Tests/ObjectLibrary/A/CMakeLists.txt b/Tests/ObjectLibrary/A/CMakeLists.txt new file mode 100644 index 0000000..e0a620e --- /dev/null +++ b/Tests/ObjectLibrary/A/CMakeLists.txt @@ -0,0 +1,17 @@ +# Add -fPIC so objects can be used in shared libraries. +# TODO: Need property for this. +if(CMAKE_SHARED_LIBRARY_C_FLAGS) + set(CMAKE_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_C_FLAGS}") +endif() + +add_definitions(-DA) + +add_custom_command( + OUTPUT a1.c + DEPENDS a1.c.in + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/a1.c.in + ${CMAKE_CURRENT_BINARY_DIR}/a1.c + ) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +add_library(A OBJECT a1.c a2.c) diff --git a/Tests/ObjectLibrary/A/a.h b/Tests/ObjectLibrary/A/a.h new file mode 100644 index 0000000..6bfbc82 --- /dev/null +++ b/Tests/ObjectLibrary/A/a.h @@ -0,0 +1,6 @@ +#ifndef A +# error "A not defined" +#endif +#ifdef B +# error "B must not be defined" +#endif diff --git a/Tests/ObjectLibrary/A/a1.c.in b/Tests/ObjectLibrary/A/a1.c.in new file mode 100644 index 0000000..d1eaf58 --- /dev/null +++ b/Tests/ObjectLibrary/A/a1.c.in @@ -0,0 +1,2 @@ +#include "a.h" +int a1(void) { return 0; } diff --git a/Tests/ObjectLibrary/A/a2.c b/Tests/ObjectLibrary/A/a2.c new file mode 100644 index 0000000..d8f225e --- /dev/null +++ b/Tests/ObjectLibrary/A/a2.c @@ -0,0 +1,2 @@ +#include "a.h" +int a2(void) { return 0; } diff --git a/Tests/ObjectLibrary/B/CMakeLists.txt b/Tests/ObjectLibrary/B/CMakeLists.txt new file mode 100644 index 0000000..498d45d --- /dev/null +++ b/Tests/ObjectLibrary/B/CMakeLists.txt @@ -0,0 +1,15 @@ +if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6") + # VS 6 generator does not use per-target object locations. + set(vs6 _vs6) +endif() + +# Add -fPIC so objects can be used in shared libraries. +# TODO: Need property for this. +if(CMAKE_SHARED_LIBRARY_C_FLAGS) + set(CMAKE_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_C_FLAGS}") +endif() + +add_definitions(-DB) +add_library(B OBJECT b1.c b2.c) +add_library(Bexport OBJECT b1${vs6}.c b2${vs6}.c) +set_property(TARGET Bexport PROPERTY COMPILE_DEFINITIONS Bexport) diff --git a/Tests/ObjectLibrary/B/b.h b/Tests/ObjectLibrary/B/b.h new file mode 100644 index 0000000..632004d --- /dev/null +++ b/Tests/ObjectLibrary/B/b.h @@ -0,0 +1,11 @@ +#ifdef A +# error "A must not be defined" +#endif +#ifndef B +# error "B not defined" +#endif +#if defined(_WIN32) && defined(Bexport) +# define EXPORT_B __declspec(dllexport) +#else +# define EXPORT_B +#endif diff --git a/Tests/ObjectLibrary/B/b1.c b/Tests/ObjectLibrary/B/b1.c new file mode 100644 index 0000000..fdeffe4 --- /dev/null +++ b/Tests/ObjectLibrary/B/b1.c @@ -0,0 +1,2 @@ +#include "b.h" +EXPORT_B int b1(void) { return 0; } diff --git a/Tests/ObjectLibrary/B/b1_vs6.c b/Tests/ObjectLibrary/B/b1_vs6.c new file mode 100644 index 0000000..b606e10 --- /dev/null +++ b/Tests/ObjectLibrary/B/b1_vs6.c @@ -0,0 +1 @@ +#include "b1.c" diff --git a/Tests/ObjectLibrary/B/b2.c b/Tests/ObjectLibrary/B/b2.c new file mode 100644 index 0000000..6e0d17c --- /dev/null +++ b/Tests/ObjectLibrary/B/b2.c @@ -0,0 +1,2 @@ +#include "b.h" +EXPORT_B int b2(void) { return 0; } diff --git a/Tests/ObjectLibrary/B/b2_vs6.c b/Tests/ObjectLibrary/B/b2_vs6.c new file mode 100644 index 0000000..d96a43e --- /dev/null +++ b/Tests/ObjectLibrary/B/b2_vs6.c @@ -0,0 +1 @@ +#include "b2.c" diff --git a/Tests/ObjectLibrary/CMakeLists.txt b/Tests/ObjectLibrary/CMakeLists.txt new file mode 100644 index 0000000..1a07d1d --- /dev/null +++ b/Tests/ObjectLibrary/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8) +project(ObjectLibrary C) + +add_subdirectory(A) +add_subdirectory(B) + +add_library(Cstatic STATIC c.c $ $) +add_executable(UseCstatic main.c) +target_link_libraries(UseCstatic Cstatic) + +add_library(Cshared SHARED c.c $ $) +add_executable(UseCshared main.c) +set_property(TARGET UseCshared PROPERTY COMPILE_DEFINITIONS SHARED_C) +target_link_libraries(UseCshared Cshared) + +add_executable(UseCinternal main.c c.c $ $) diff --git a/Tests/ObjectLibrary/c.c b/Tests/ObjectLibrary/c.c new file mode 100644 index 0000000..968095b --- /dev/null +++ b/Tests/ObjectLibrary/c.c @@ -0,0 +1,19 @@ +#if defined(_WIN32) && defined(Cshared_EXPORTS) +# define EXPORT_C __declspec(dllexport) +#else +# define EXPORT_C +#endif + +extern int a1(void); +extern int a2(void); +extern int b1(void); +extern int b2(void); +EXPORT_C int c(void) +{ + return 0 + + a1() + + a2() + + b1() + + b2() + ; +} diff --git a/Tests/ObjectLibrary/main.c b/Tests/ObjectLibrary/main.c new file mode 100644 index 0000000..6819f1c --- /dev/null +++ b/Tests/ObjectLibrary/main.c @@ -0,0 +1,16 @@ +#if defined(_WIN32) && defined(SHARED_C) +# define IMPORT_C __declspec(dllimport) +#else +# define IMPORT_C +#endif +extern IMPORT_C int b1(void); +extern IMPORT_C int b2(void); +extern IMPORT_C int c(void); +int main(void) +{ + return 0 + + c() + + b1() + + b2() + ; +} -- cgit v0.12