Skip to content

Commit 8601795

Browse files
committed
Use libbsd when needed
1 parent 4ff6fbd commit 8601795

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ project(tlc LANGUAGES C)
44

55
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
66

7-
# find_path(HAVE_SYS_SYSMACROS_H sys/sysmacros.h)
8-
# if (HAVE_SYS_SYSMACROS_H)
9-
# add_compile_definitions(HAVE_SYS_SYSMACROS_H)
10-
# endif()
7+
if (CMAKE_BUILD_TYPE STREQUAL Debug)
8+
add_definitions(-DDEBUG)
9+
endif()
1110

1211
add_executable(tlc tlc.c)
12+
13+
if (APPLE OR CMAKE_SYSTEM_NAME MATCHES ".*BSD.*")
14+
message(STATUS "It looks you're running BSD system and do not need libbsd")
15+
else()
16+
find_package(BSD REQUIRED)
17+
list(APPEND BSD_DEFINITIONS -DLIBBSD_OVERLAY)
18+
list(APPEND BSD_DEFINITIONS -D_BSD_SOURCE=1 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE=1 -D_XOPEN_SOURCE=700)
19+
target_compile_definitions(tlc PRIVATE ${BSD_DEFINITIONS})
20+
target_include_directories(tlc BEFORE PRIVATE ${BSD_INCLUDE_DIRS})
21+
target_link_libraries(tlc ${BSD_LIBRARIES})
22+
endif()
23+
1324
install(TARGETS tlc RUNTIME DESTINATION bin)

cmake/FindBSD.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# copied from Swift-2, Apache 2.0 licensed
2+
# author: https://github.com/stormbrew
3+
# source: https://github.com/hsavit1/swift-2/blob/master/cmake/modules/FindBSD.cmake
4+
5+
# Find libbsd
6+
7+
find_package(PkgConfig)
8+
pkg_check_modules(PC_BSD QUIET bsd)
9+
set(BSD_DEFINITIONS ${PC_BSD_CFLAGS_OTHER})
10+
11+
find_path(BSD_INCLUDE_DIR bsd/stdlib.h
12+
HINTS ${PC_BSD_INCLUDEDIR} ${PC_BSD_INCLUDE_DIRS})
13+
set(BSD_INCLUDE_DIRS ${BSD_INCLUDE_DIR}/bsd)
14+
15+
find_library(BSD_LIBRARY NAMES bsd
16+
HINTS ${PC_BSD_LIBDIR} ${PC_BSD_LIBRARY_DIRS})
17+
set(BSD_LIBRARIES ${BSD_LIBRARY})
18+
19+
set(BSD_REQUIRED BSD_LIBRARY BSD_INCLUDE_DIR)
20+
21+
include(FindPackageHandleStandardArgs)
22+
find_package_handle_standard_args(BSD DEFAULT_MSG ${BSD_REQUIRED})
23+
24+
mark_as_advanced(${BSD_REQUIRED})

compat.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 1982, 1986, 1993
3+
* The Regents of the University of California. All rights reserved.
4+
* Copyright (c) 2021 Vadim Zhukov <zhuk@openbsd.org>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. Neither the name of the University nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28+
* SUCH DAMAGE.
29+
*
30+
* @(#)time.h 8.2 (Berkeley) 7/10/94
31+
*/
32+
33+
#ifndef TLC_COMPAT_H
34+
#define TLC_COMPAT_H
35+
36+
/* Operations on timespecs. */
37+
38+
#ifndef timespecclear
39+
#define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
40+
#endif
41+
42+
#ifndef timespecisset
43+
#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
44+
#endif
45+
46+
#ifndef timespecisvalid
47+
#define timespecisvalid(tsp) \
48+
((tsp)->tv_nsec >= 0 && (tsp)->tv_nsec < 1000000000L)
49+
#endif
50+
51+
#ifndef timespeccmp
52+
#define timespeccmp(tsp, usp, cmp) \
53+
(((tsp)->tv_sec == (usp)->tv_sec) ? \
54+
((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
55+
((tsp)->tv_sec cmp (usp)->tv_sec))
56+
#endif
57+
58+
#ifndef timespecadd
59+
#define timespecadd(tsp, usp, vsp) \
60+
do { \
61+
(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
62+
(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
63+
if ((vsp)->tv_nsec >= 1000000000L) { \
64+
(vsp)->tv_sec++; \
65+
(vsp)->tv_nsec -= 1000000000L; \
66+
} \
67+
} while (0)
68+
#endif
69+
70+
#ifndef timespecsub
71+
#define timespecsub(tsp, usp, vsp) \
72+
do { \
73+
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
74+
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
75+
if ((vsp)->tv_nsec < 0) { \
76+
(vsp)->tv_sec--; \
77+
(vsp)->tv_nsec += 1000000000L; \
78+
} \
79+
} while (0)
80+
#endif
81+
82+
#endif

tlc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
#include <time.h>
3030
#include <unistd.h>
3131

32-
#ifndef __dead
33-
#define __dead
34-
#endif
32+
#include "compat.h"
3533

3634
// user-defined parameters
3735
bool debug, use_format, passthrough;

0 commit comments

Comments
 (0)