Merge topic 'allow-OBJECT-library-extra-sources'

2c470b77 add_library: Allow arbitrary non-linked sources in OBJECT libraries
de4f3e2c Tests: Fix broken dependency in ObjectLibrary test
54bf5858 Tests: Resolve TODO comment left in ObjectLibrary test
This commit is contained in:
Brad King 2014-07-09 10:02:45 -04:00 committed by CMake Topic Stage
commit e187351c5f
8 changed files with 30 additions and 18 deletions

View File

@ -94,7 +94,8 @@ object library name. For example:
will include objlib's object files in a library and an executable will include objlib's object files in a library and an executable
along with those compiled from their own sources. Object libraries along with those compiled from their own sources. Object libraries
may contain only sources (and headers) that compile to object files. may contain only sources that compile, header files, and other files
that would not affect linking of a normal library (e.g. ``.txt``).
They may contain custom commands generating such sources, but not They may contain custom commands generating such sources, but not
``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Object libraries ``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Object libraries
cannot be imported, exported, installed, or linked. Some native build cannot be imported, exported, installed, or linked. Some native build

View File

@ -0,0 +1,6 @@
allow-OBJECT-library-extra-sources
----------------------------------
* :ref:`Object Libraries` may now have extra sources that do not
compile to object files so long as they would not affect linking
of a normal library (e.g. ``.dat`` is okay but not ``.def``).

View File

@ -38,7 +38,8 @@ void reportBadObjLib(std::vector<cmSourceFile*> const& badObjLib,
{ {
e << " " << (*i)->GetLocation().GetName() << "\n"; e << " " << (*i)->GetLocation().GetName() << "\n";
} }
e << "but may contain only headers and sources that compile."; e << "but may contain only sources that compile, header files, and "
"other files that would not affect linking of a normal library.";
cm->IssueMessage(cmake::FATAL_ERROR, e.str(), cm->IssueMessage(cmake::FATAL_ERROR, e.str(),
target->GetBacktrace()); target->GetBacktrace());
} }
@ -205,10 +206,6 @@ struct TagVisitor
else else
{ {
DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf); DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
if(this->IsObjLib && ext != "txt")
{
this->BadObjLibFiles.push_back(sf);
}
} }
} }
}; };

View File

@ -1,18 +1,26 @@
project(ObjectLibraryA C) project(ObjectLibraryA C)
# Add -fPIC so objects can be used in shared libraries. # Add -fPIC so objects can be used in shared libraries.
# TODO: Need property for this. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(CMAKE_SHARED_LIBRARY_C_FLAGS AND NOT WATCOM)
set(CMAKE_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_C_FLAGS}")
endif()
add_definitions(-DA_DEF) add_definitions(-DA_DEF)
add_custom_command( add_custom_command(
OUTPUT a1.c OUTPUT a1.c
DEPENDS a1.c.in DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/a1.c.in
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/a1.c.in COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/a1.c.in
${CMAKE_CURRENT_BINARY_DIR}/a1.c ${CMAKE_CURRENT_BINARY_DIR}/a1.c
) )
add_library(A OBJECT a1.c a2.c) # Remove the custom command output to be sure it runs in an
# incremental test. Skip this on VS 6 because it sometimes
# re-runs CMake after the custom command runs.
if(NOT CMAKE_GENERATOR STREQUAL "Visual Studio 6")
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/a.cmake)
endif()
add_custom_command(
OUTPUT a.cmake
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/a.cmake
)
add_library(A OBJECT a1.c a2.c a.cmake)
target_include_directories(A PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(A PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -5,10 +5,7 @@ if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
endif() endif()
# Add -fPIC so objects can be used in shared libraries. # Add -fPIC so objects can be used in shared libraries.
# TODO: Need property for this. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(CMAKE_SHARED_LIBRARY_C_FLAGS AND NOT WATCOM)
set(CMAKE_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_C_FLAGS}")
endif()
add_library(B OBJECT b1.c b2.c) add_library(B OBJECT b1.c b2.c)
target_include_directories(B PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(B PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -12,6 +12,7 @@ add_library(Cshared SHARED c.c $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:Bexport>)
add_executable(UseCshared main.c) add_executable(UseCshared main.c)
set_property(TARGET UseCshared PROPERTY COMPILE_DEFINITIONS SHARED_C) set_property(TARGET UseCshared PROPERTY COMPILE_DEFINITIONS SHARED_C)
target_link_libraries(UseCshared Cshared) target_link_libraries(UseCshared Cshared)
add_custom_command(TARGET UseCshared POST_BUILD COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/A/a.cmake)
add_executable(UseCinternal main.c c.c $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>) add_executable(UseCinternal main.c c.c $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)

View File

@ -3,6 +3,7 @@ CMake Error at BadObjSource1.cmake:1 \(add_library\):
bad.def bad.def
but may contain only headers and sources that compile. but may contain only sources that compile, header files, and other files
that would not affect linking of a normal library.
Call Stack \(most recent call first\): Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\) CMakeLists.txt:3 \(include\)

View File

@ -3,6 +3,7 @@ CMake Error at BadObjSource2.cmake:1 \(add_library\):
bad.obj bad.obj
but may contain only headers and sources that compile. but may contain only sources that compile, header files, and other files
that would not affect linking of a normal library.
Call Stack \(most recent call first\): Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\) CMakeLists.txt:3 \(include\)