Merge branch 'object-library' into xcode-object-library

This commit is contained in:
Brad King 2012-03-16 10:15:42 -04:00
commit eb24c99c4d
72 changed files with 360 additions and 0 deletions

View File

@ -112,6 +112,26 @@ public:
"(and its per-configuration version IMPORTED_LOCATION_<CONFIG>) "
"which specifies the location of the main library file on disk. "
"See documentation of the IMPORTED_* properties for more information."
"\n"
"The signature\n"
" add_library(<name> OBJECT <src>...)\n"
"creates a special \"object library\" target. "
"An object library compiles source files but does not archive or link "
"their object files into a library. "
"Instead other targets created by add_library or add_executable may "
"reference the objects using an expression of the form "
"$<TARGET_OBJECTS:objlib> as a source, where \"objlib\" is the "
"object library name. "
"For example:\n"
" add_library(... $<TARGET_OBJECTS:objlib> ...)\n"
" add_executable(... $<TARGET_OBJECTS:objlib> ...)\n"
"will include objlib's object files in a library and an executable "
"along with those compiled from their own sources. "
"Object libraries may contain only sources (and headers) that compile "
"to object files. "
"They may contain custom commands generating such sources, but not "
"PRE_BUILD, PRE_LINK, or POST_BUILD commands. "
"Object libraries cannot be imported, exported, installed, or linked."
;
}

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,6 @@
#ifndef A
# error "A not defined"
#endif
#ifdef B
# error "B must not be defined"
#endif

View File

@ -0,0 +1,2 @@
#include "a.h"
int a1(void) { return 0; }

View File

@ -0,0 +1,2 @@
#include "a.h"
int a2(void) { return 0; }

View File

@ -0,0 +1,5 @@
EXPORTS
a1
a2
b1
b2

View File

@ -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)

11
Tests/ObjectLibrary/B/b.h Normal file
View File

@ -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

View File

@ -0,0 +1,2 @@
#include "b.h"
EXPORT_B int b1(void) { return 0; }

View File

@ -0,0 +1 @@
#include "b1.c"

View File

@ -0,0 +1,2 @@
#include "b.h"
EXPORT_B int b2(void) { return 0; }

View File

@ -0,0 +1 @@
#include "b2.c"

View File

@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 2.8)
project(ObjectLibrary C)
add_subdirectory(A)
add_subdirectory(B)
add_library(Cstatic STATIC c.c $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)
add_executable(UseCstatic main.c)
target_link_libraries(UseCstatic Cstatic)
add_library(Cshared SHARED c.c $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:Bexport>)
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 $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)
if("${CMAKE_GENERATOR}" MATCHES "^Visual Studio (6|7|7 .NET 2003)$")
# VS 6 and 7 generators do not add objects as sources so we need a
# dummy object to convince the IDE to build the targets below.
set(dummy dummy.obj) # In MinGW: gcc -c dummy.c -o dummy.obj
elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
# Xcode does not seem to support targets without sources.
set(dummy dummy.c)
endif()
# Test static library without its own sources.
add_library(ABstatic STATIC ${dummy} $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)
add_executable(UseABstatic mainAB.c)
target_link_libraries(UseABstatic ABstatic)
# Test module definition file to export object library symbols in the test
# below if the platform needs and supports it.
set(ABshared_SRCS $<TARGET_OBJECTS:A>)
if(CMAKE_LINK_DEF_FILE_FLAG OR NOT WIN32)
list(APPEND ABshared_SRCS $<TARGET_OBJECTS:B> AB.def)
else()
set(NO_A NO_A)
list(APPEND ABshared_SRCS $<TARGET_OBJECTS:Bexport>)
endif()
# Test shared library without its own sources.
add_library(ABshared SHARED ${dummy} ${ABshared_SRCS})
add_executable(UseABshared mainAB.c)
set_property(TARGET UseABshared PROPERTY COMPILE_DEFINITIONS SHARED_B ${NO_A})
target_link_libraries(UseABshared ABshared)
# Test executable without its own sources.
add_library(ABmain OBJECT mainAB.c)
add_executable(UseABinternal ${dummy}
$<TARGET_OBJECTS:ABmain> $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>
)

19
Tests/ObjectLibrary/c.c Normal file
View File

@ -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()
;
}

View File

@ -0,0 +1 @@
int dummy(void) {return 0;}

Binary file not shown.

View File

@ -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()
;
}

View File

@ -0,0 +1,22 @@
#if defined(_WIN32) && defined(SHARED_B)
# define IMPORT_B __declspec(dllimport)
#else
# define IMPORT_B
#endif
extern IMPORT_B int b1(void);
extern IMPORT_B int b2(void);
#ifndef NO_A
extern int a1(void);
extern int a2(void);
#endif
int main(void)
{
return 0
#ifndef NO_A
+ a1()
+ a2()
#endif
+ b1()
+ b2()
;
}

View File

@ -40,5 +40,7 @@ macro(add_RunCMake_test test)
)
endmacro()
add_RunCMake_test(ObjectLibrary)
add_RunCMake_test(build_command)
add_RunCMake_test(find_package)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,8 @@
CMake Error at BadObjSource1.cmake:1 \(add_library\):
OBJECT library "A" contains:
bad.def
but may contain only headers and sources that compile.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1 @@
add_library(A OBJECT a.c bad.def)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,8 @@
CMake Error at BadObjSource2.cmake:1 \(add_library\):
OBJECT library "A" contains:
bad.obj
but may contain only headers and sources that compile.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1 @@
add_library(A OBJECT a.c bad.obj)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,6 @@
CMake Error at BadSourceExpression1.cmake:1 \(add_library\):
Unrecognized generator expression:
\$<BAD_EXPRESSION>
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1 @@
add_library(A STATIC a.c $<BAD_EXPRESSION>)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at BadSourceExpression2.cmake:1 \(add_library\):
Objects of target "DoesNotExist" referenced but no such target exists.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1 @@
add_library(A STATIC a.c $<TARGET_OBJECTS:DoesNotExist>)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at BadSourceExpression3.cmake:2 \(add_library\):
Objects of target "NotObjLib" referenced but is not an OBJECT library.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
add_library(NotObjLib STATIC a.c)
add_library(A STATIC a.c $<TARGET_OBJECTS:NotObjLib>)

View File

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 2.8)
project(${RunCMake_TEST} C)
include(${RunCMake_TEST}.cmake)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at Export.cmake:2 \(export\):
export given OBJECT library "A" which may not be exported.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
add_library(A OBJECT a.c)
export(TARGETS A FILE AExport.cmake)

View File

@ -0,0 +1,15 @@
enable_language(CXX)
add_library(A OBJECT a.cxx)
add_library(B STATIC a.c $<TARGET_OBJECTS:A>)
# Verify that object library languages are propagated.
export(TARGETS B NAMESPACE Exp FILE BExport.cmake)
include(${CMAKE_CURRENT_BINARY_DIR}/BExport.cmake)
get_property(configs TARGET ExpB PROPERTY IMPORTED_CONFIGURATIONS)
foreach(c ${configs})
get_property(langs TARGET ExpB PROPERTY IMPORTED_LINK_INTERFACE_LANGUAGES_${c})
list(FIND langs CXX pos)
if(${pos} LESS 0)
message(FATAL_ERROR "Target export does not list object library languages.")
endif()
endforeach()

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at Import.cmake:1 \(add_library\):
The OBJECT library type may not be used for IMPORTED libraries.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1 @@
add_library(A OBJECT IMPORTED)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at Install.cmake:2 \(install\):
install TARGETS given OBJECT library "A" which may not be installed.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
add_library(A OBJECT a.c)
install(TARGETS A DESTINATION lib)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at LinkObjLHS.cmake:2 \(target_link_libraries\):
Object library target "AnObjLib" may not link to anything.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
add_library(AnObjLib OBJECT a.c)
target_link_libraries(AnObjLib OtherLib)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,6 @@
CMake Error at LinkObjRHS1.cmake:3 \(target_link_libraries\):
Target "AnObjLib" of type OBJECT_LIBRARY may not be linked into another
target. One may link only to STATIC or SHARED libraries, or to executables
with the ENABLE_EXPORTS property set.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,3 @@
add_library(A STATIC a.c)
add_library(AnObjLib OBJECT a.c)
target_link_libraries(A AnObjLib)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,6 @@
CMake Error at LinkObjRHS2.cmake:1 \(add_library\):
Target "A" links to OBJECT library "AnObjLib" but this is not allowed. One
may link only to STATIC or SHARED libraries, or to executables with the
ENABLE_EXPORTS property set.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,3 @@
add_library(A SHARED a.c)
target_link_libraries(A AnObjLib)
add_library(AnObjLib OBJECT a.c)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at ObjWithObj.cmake:2 \(add_library\):
Only executables and non-OBJECT libraries may reference target objects.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
add_library(A OBJECT a.c)
add_library(B OBJECT $<TARGET_OBJECTS:A>)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,5 @@
CMake Error at PostBuild.cmake:2 \(add_custom_command\):
Target "A" is an OBJECT library that may not have PRE_BUILD, PRE_LINK, or
POST_BUILD commands.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,4 @@
add_library(A OBJECT a.c)
add_custom_command(TARGET A POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "A post-build"
)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,5 @@
CMake Error at PreBuild.cmake:2 \(add_custom_command\):
Target "A" is an OBJECT library that may not have PRE_BUILD, PRE_LINK, or
POST_BUILD commands.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,4 @@
add_library(A OBJECT a.c)
add_custom_command(TARGET A PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "A pre-build"
)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,5 @@
CMake Error at PreLink.cmake:2 \(add_custom_command\):
Target "A" is an OBJECT library that may not have PRE_BUILD, PRE_LINK, or
POST_BUILD commands.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,4 @@
add_library(A OBJECT a.c)
add_custom_command(TARGET A PRE_LINK
COMMAND ${CMAKE_COMMAND} -E echo "A pre-link"
)

View File

@ -0,0 +1,18 @@
include(RunCMake)
run_cmake(BadSourceExpression1)
run_cmake(BadSourceExpression2)
run_cmake(BadSourceExpression3)
run_cmake(BadObjSource1)
run_cmake(BadObjSource2)
run_cmake(Export)
run_cmake(ExportLanguages)
run_cmake(Import)
run_cmake(Install)
run_cmake(LinkObjLHS)
run_cmake(LinkObjRHS1)
run_cmake(LinkObjRHS2)
run_cmake(ObjWithObj)
run_cmake(PostBuild)
run_cmake(PreBuild)
run_cmake(PreLink)

View File

@ -0,0 +1 @@
int a(void) { return 0; }

View File

@ -0,0 +1 @@
extern "C" int acxx(void) { return 0; }

View File

View File