Skip to content

Commit

Permalink
New CMake and silence some compilewarning on ARM64
Browse files Browse the repository at this point in the history
  • Loading branch information
palerikm committed Feb 13, 2023
1 parent 32f7a66 commit a588abd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 21 deletions.
70 changes: 55 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,63 @@ project(${LIB_NAME} C)

set(CMAKE_C_STANDARD 99)

include_directories(include)

set( ${LIB_NAME}_srcs
src/sockaddrutil.c
include/sockaddrutil.h )
add_subdirectory(src)

add_library(${LIB_NAME} STATIC ${${LIB_NAME}_srcs} )
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
cmake_minimum_required(VERSION 3.18)
option(SOCKADDRUTIL_BUILD_TESTS "whether or not tests should be built" ON)
option(SOCKADDRUTIL_BUILD_PACKAGE "whether or not the package should be built" ON)
option(SOCKADDRUTIL_INSTALL "whether or not to enable the install rule" ON)

install(TARGETS ${LIB_NAME} EXPORT ${LIB_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION lib
INCLUDES destination include)
if(SOCKADDRUTIL_BUILD_PACKAGE)
set(package_files include/ src/ cmake/ CMakeLists.txt LICENSE)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/sokaddrutil-src.zip
COMMAND ${CMAKE_COMMAND} -E tar c ${CMAKE_CURRENT_BINARY_DIR}/sokaddrutil-src.zip --format=zip -- ${package_files}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${package_files})
add_custom_target(sokaddrutil_package DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/sokaddrutil-src.zip)
endif()

install(EXPORT ${LIB_NAME}Targets
FILE ${${LIB_NAME}Targets}.cmake
NAMESPACE ${LIB_NAME}
DESTINATION lib/cmake/${LIB_NAME})
if(SOCKADDRUTIL_BUILD_TESTS)
set(DOCTEST_NO_INSTALL ON)
enable_testing()
add_subdirectory(test)
endif()

add_subdirectory ( test )

if(SOCKADDRUTIL_ENABLE_INSTALL)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

install(TARGETS sockaddrutil
EXPORT ${PROJECT_NAME}Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE ${LIB_NAME}
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

configure_package_config_file(
cmake/sockaddrutilConfig.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

# YYYY.MM.N1 is compatible with YYYY.MM.N2.
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
COMPATIBILITY SameMinorVersion)

install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

install(DIRECTORY include
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN "*.h")
endif()
endif()
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
.target(name: "sockaddrutil",
dependencies: [],
path: ".",
exclude: ["test"],
exclude: ["test", "src/CMakeLists.txt"],
sources: ["src"],
publicHeadersPath: "include",
cSettings: [
Expand Down
5 changes: 5 additions & 0 deletions cmake/sockaddrutilConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# socaddrutil CMake configuration file.

@PACKAGE_INIT@

include ("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
2 changes: 1 addition & 1 deletion include/sockaddrutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ sockaddr_isAddrDeprecated(const struct sockaddr* sa,
const char*
sockaddr_toString(const struct sockaddr* sa,
char* dest,
size_t destlen,
socklen_t destlen,
bool addport);

/*
Expand Down
20 changes: 20 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

get_filename_component(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/../include ABSOLUTE)
set(header_files ${include_dir}/sockaddrutil.h PARENT_SCOPE)
#get_filename_component(src_dir ${CMAKE_CURRENT_SOURCE_DIR}/../src ABSOLUTE)
set( source_files sockaddrutil.c)

include(GNUInstallDirs)



# Core library.
add_library(sockaddrutil INTERFACE ${headers_files})
target_sources(sockaddrutil PUBLIC ${source_files})
##add_alias(lexy::core lexy_core)
##target_link_libraries(lexy_core INTERFACE _lexy_base)
target_include_directories(sockaddrutil SYSTEM INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

7 changes: 3 additions & 4 deletions src/sockaddrutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <unistd.h>



#include "sockaddrutil.h"

void
Expand Down Expand Up @@ -530,7 +529,7 @@ sockaddr_isAddrDeprecated(const struct sockaddr* sa,
const char*
sockaddr_toString(const struct sockaddr* sa,
char* dest,
size_t destlen,
socklen_t destlen,
bool addport)
{
if (sa->sa_family == AF_INET)
Expand All @@ -548,7 +547,7 @@ sockaddr_toString(const struct sockaddr* sa,
inet_ntop(AF_INET, &(sa4->sin_addr), dest, destlen);
if (addport)
{
int r = strlen(dest);
unsigned long r = strlen(dest);
sprintf( dest + r, ":%d", ntohs(sa4->sin_port) );
}
return dest;
Expand All @@ -563,7 +562,7 @@ sockaddr_toString(const struct sockaddr* sa,
}
else
{
int r;
unsigned long r;
const struct sockaddr_in6* sa6 = (const struct sockaddr_in6*)sa;
if (addport)
{
Expand Down

0 comments on commit a588abd

Please sign in to comment.