build: fix libcurltool with cmake and tunits, related tidy-ups

Sync how libcurltool is built in different modes and build systems.

cmake:
- build libcurltool with curlx when building shared libcurl.
  To make it possible to use standard libcurl when linking tunits.
  Also syncing this with autotools.
  The remaining difference is that cmake allows to select shared or
  static for curl tool and tests/examples independently.
- fix to link with libcurl instead of libcurlu.
  To sync with autotools and to link with the standard libcurl for
  tool unit tests.
- fix `source_group()` to always include curlx sources.
- add missing 'curlx header files' source group.

autotools:
- build libcurltool without curlx when building static libcurl in
  non-unity builds.
  To avoid double compilation, just to be thrown away at link time.
  Also to sync with unity builds.

both:
- sync source order between autotools and cmake.
- make sure to pass all headers with both autotools and cmake.
  This is a no-op with cmake. Maybe a future patch should make sure
  to not pass those to remove that noise.

Ref: #17696

Closes #17727
This commit is contained in:
Viktor Szakats 2025-06-23 23:29:59 +02:00
parent 1ec0da0149
commit f4649425f2
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
4 changed files with 32 additions and 24 deletions

View file

@ -66,16 +66,29 @@ if(CURL_CA_EMBED_SET)
endif()
endif()
# Get CURL_FILES, CURL_CFILES, CURL_HFILES, CURLX_CFILES, CURLX_HFILES, CURL_RCFILES variables
# Get CURL_CFILES, CURL_HFILES, CURLX_CFILES, CURLX_HFILES, CURL_RCFILES variables
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
source_group("curl source files" FILES ${CURL_CFILES} ${_curl_cfiles_gen})
source_group("curl header files" FILES ${CURL_HFILES} ${_curl_hfiles_gen})
source_group("curlx source files" FILES ${CURLX_CFILES})
source_group("curlx header files" FILES ${CURLX_HFILES})
if(WIN32)
list(APPEND CURL_CFILES ${CURL_RCFILES})
endif()
set(_curlx_cfiles_lib ${CURLX_CFILES})
set(_curlx_hfiles_lib ${CURLX_HFILES})
if(LIB_SELECTED STREQUAL LIB_STATIC)
set(_curlx_cfiles_lib "")
set(_curlx_hfiles_lib "")
endif()
if(BUILD_STATIC_CURL)
set(CURLX_CFILES "")
set(CURLX_HFILES "")
endif()
set_property(DIRECTORY APPEND PROPERTY INCLUDE_DIRECTORIES
@ -84,15 +97,17 @@ set_property(DIRECTORY APPEND PROPERTY INCLUDE_DIRECTORIES
"${CMAKE_CURRENT_SOURCE_DIR}" # for "tool_hugehelp.c"
)
add_executable(${EXE_NAME} ${CURL_CFILES} ${_curl_cfiles_gen} ${CURLX_CFILES} ${CURL_HFILES} ${_curl_hfiles_gen})
# Build curl executable
add_executable(${EXE_NAME} ${CURL_CFILES} ${CURL_HFILES} ${_curl_cfiles_gen} ${_curl_hfiles_gen} ${CURLX_CFILES} ${CURLX_HFILES})
target_compile_definitions(${EXE_NAME} PRIVATE ${_curl_definitions})
target_link_libraries(${EXE_NAME} ${LIB_SELECTED_FOR_EXE} ${CURL_LIBS})
add_executable(${PROJECT_NAME}::${EXE_NAME} ALIAS ${EXE_NAME})
add_executable(curlinfo EXCLUDE_FROM_ALL "curlinfo.c")
# special libcurltool library just for unittests
add_library(curltool STATIC EXCLUDE_FROM_ALL ${CURL_CFILES} ${CURL_HFILES})
add_library(curltool STATIC EXCLUDE_FROM_ALL ${CURL_CFILES} ${CURL_HFILES} ${_curlx_cfiles_lib} ${_curlx_hfiles_lib})
target_compile_definitions(curltool PUBLIC "CURL_STATICLIB" "UNITTESTS")
target_link_libraries(curltool PRIVATE ${CURL_LIBS})
set_target_properties(curltool PROPERTIES C_CLANG_TIDY "")
@ -109,13 +124,6 @@ if(ENABLE_UNICODE AND MINGW AND NOT MINGW32CE)
endif()
endif()
source_group("curlX source files" FILES ${CURLX_CFILES})
source_group("curl source files" FILES ${CURL_CFILES} ${_curl_cfiles_gen})
source_group("curl header files" FILES ${CURL_HFILES} ${_curl_hfiles_gen})
# Build curl executable
target_link_libraries(${EXE_NAME} ${LIB_SELECTED_FOR_EXE} ${CURL_LIBS})
################################################################################
install(TARGETS ${EXE_NAME} EXPORT ${TARGETS_EXPORT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

View file

@ -26,7 +26,7 @@ AUTOMAKE_OPTIONS = foreign nostdinc
# remove targets if the command fails
.DELETE_ON_ERROR:
# Get CURL_FILES, CURL_CFILES, CURL_HFILES, CURLX_CFILES, CURLX_HFILES, CURL_RCFILES variables
# Get CURL_CFILES, CURL_HFILES, CURLX_CFILES, CURLX_HFILES, CURL_RCFILES variables
include Makefile.inc
EXTRA_DIST = mk-file-embed.pl mkhelp.pl \
@ -71,21 +71,24 @@ curl_cfiles_gen =
curl_hfiles_gen =
CLEANFILES =
if USE_UNITY
if USE_CPPFLAG_CURL_STATICLIB
curlx_src =
curlx_csrc =
curlx_hsrc =
else
# These are part of the libcurl static lib. Add them here when linking shared.
curlx_src = $(CURLX_CFILES)
curlx_csrc = $(CURLX_CFILES)
curlx_hsrc = $(CURLX_HFILES)
endif
curltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(curl_cfiles_gen) $(curlx_src)
@PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(CURL_CFILES) $(curl_cfiles_gen) $(curlx_src) > curltool_unity.c
if USE_UNITY
curltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(CURL_HFILES) $(curl_cfiles_gen) $(curl_hfiles_gen) $(curlx_csrc) $(curlx_hsrc)
@PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(CURL_CFILES) $(CURL_HFILES) $(curl_cfiles_gen) $(curl_hfiles_gen) $(curlx_csrc) $(curlx_hsrc) > curltool_unity.c
nodist_curl_SOURCES = curltool_unity.c
curl_SOURCES =
CLEANFILES += curltool_unity.c
else
curl_SOURCES = $(CURL_FILES) $(curl_cfiles_gen) $(curl_hfiles_gen)
curl_SOURCES = $(CURL_CFILES) $(CURL_HFILES) $(curl_cfiles_gen) $(curl_hfiles_gen) $(curlx_csrc) $(curlx_hsrc)
endif
if HAVE_WINDRES
curl_SOURCES += $(CURL_RCFILES)
@ -109,14 +112,14 @@ libcurltool_la_CPPFLAGS = $(AM_CPPFLAGS) -DCURL_STATICLIB -DUNITTESTS
libcurltool_la_CFLAGS =
libcurltool_la_LDFLAGS = -static $(LIBCURL_PC_LIBS_PRIVATE)
if USE_UNITY
libcurltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(curlx_src)
@PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(CURL_CFILES) $(curlx_src) > libcurltool_unity.c
libcurltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(CURL_HFILES) $(curlx_csrc) $(curlx_hsrc)
@PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(CURL_CFILES) $(CURL_HFILES) $(curlx_csrc) $(curlx_hsrc) > libcurltool_unity.c
nodist_libcurltool_la_SOURCES = libcurltool_unity.c
libcurltool_la_SOURCES =
CLEANFILES += libcurltool_unity.c
else
libcurltool_la_SOURCES = $(CURL_FILES)
libcurltool_la_SOURCES = $(CURL_CFILES) $(CURL_HFILES) $(curlx_csrc) $(curlx_hsrc)
endif
endif

View file

@ -150,6 +150,3 @@ CURL_HFILES = \
var.h
CURL_RCFILES = curl.rc
# curl_SOURCES is special and gets assigned in src/Makefile.am
CURL_FILES = $(CURL_CFILES) $(CURLX_CFILES) $(CURL_HFILES)

View file

@ -36,7 +36,7 @@ add_custom_command(OUTPUT "${BUNDLE}.c"
add_executable(${BUNDLE} EXCLUDE_FROM_ALL "${BUNDLE}.c")
add_dependencies(testdeps ${BUNDLE})
target_link_libraries(${BUNDLE} curltool curlu)
target_link_libraries(${BUNDLE} curltool ${LIB_SELECTED})
target_include_directories(${BUNDLE} PRIVATE
"${PROJECT_BINARY_DIR}/lib" # for "curl_config.h"
"${PROJECT_SOURCE_DIR}/lib" # for "curl_setup.h", curlx