Skip to content

Commit b805a29

Browse files
committed
Initial commit
0 parents  commit b805a29

36 files changed

+3245
-0
lines changed

CMakeLists.txt

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#[[
2+
CMakeLists.txt - contains CMake build code for libnex
3+
Copyright 2022 The NexNix Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
There should be a copy of the License distributed in a file named
8+
LICENSE, if not, you may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License
17+
]]
18+
19+
cmake_minimum_required(VERSION 3.00)
20+
project(libnex VERSION 0.0.1)
21+
22+
# Various options
23+
option(BUILD_SHARED_LIBS "Specifies if shared libraries should be built" OFF)
24+
option(LIBNEX_ENABLE_TESTS "Specifies if the test suite should be built" OFF)
25+
option(LIBNEX_BAREMETAL "Specifies if the build should be tailored to baremetal platforms" OFF)
26+
27+
if(${LIBNEX_ENABLE_TESTS} AND NOT ${CMAKE_CROSSCOMPILING})
28+
enable_testing()
29+
endif()
30+
31+
# Find the NexNix SDK
32+
find_package(NexNixSdk REQUIRED)
33+
34+
# Include modules
35+
include(GNUInstallDirs)
36+
include(NexTest)
37+
include(CheckSymbolExists)
38+
include(CheckCSourceCompiles)
39+
include(CMakePackageConfigHelpers)
40+
41+
# See if tests should be enabled
42+
if(${LIBNEX_ENABLE_TESTS})
43+
nextest_enable_tests()
44+
endif()
45+
46+
# Check for different functions
47+
check_symbol_exists(strlcpy "string.h" HAVE_BSD_STRING)
48+
check_symbol_exists(setprogname "stdlib.h" HAVE_PROGNAME)
49+
check_symbol_exists(pthread_self "pthread.h" HAVE_PTHREAD)
50+
51+
# Configure the header
52+
configure_file(${CMAKE_SOURCE_DIR}/src/libnex_config.in.h ${CMAKE_BINARY_DIR}/libnex_config.h)
53+
configure_file(${CMAKE_SOURCE_DIR}/data/nex.pc.in ${CMAKE_BINARY_DIR}/nex.pc @ONLY)
54+
55+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
56+
${CMAKE_SOURCE_DIR}/data/nex.pc.in
57+
${CMAKE_SOURCE_DIR}/src/libnex_config.in.h
58+
${CMAKE_SOURCE_DIR}/data/LibNexConfig.cmake.in)
59+
60+
# Setup different source code types
61+
list(APPEND LIBNEX_SOURCES_ALWAYS
62+
src/list.c
63+
src/lock.c
64+
src/endian.c
65+
src/object.c)
66+
67+
list(APPEND LIBNEX_HOSTED_SOURCES
68+
src/textstream.c
69+
src/getopt.c
70+
src/error.c
71+
src/safemalloc.c)
72+
73+
# If the host lacks some functions, add them
74+
if(NOT HAVE_PROGNAME)
75+
list(APPEND LIBNEX_HOSTED_SOURCES
76+
src/progname.c)
77+
endif()
78+
79+
if(NOT HAVE_BSD_STRING)
80+
list(APPEND LIBNEX_SOURCES_ALWAYS
81+
src/strlcpy.c
82+
src/strlcat.c)
83+
endif()
84+
85+
# Create the main source code list
86+
list(APPEND LIBNEX_SOURCES ${LIBNEX_SOURCES_ALWAYS})
87+
if(NOT LIBNEX_BAREMETAL)
88+
list(APPEND LIBNEX_SOURCES ${LIBNEX_HOSTED_SOURCES})
89+
endif()
90+
91+
# Create the include files list
92+
list(APPEND LIBNEX_HEADERS_ALWAYS
93+
${CMAKE_SOURCE_DIR}/include/libnex/container.h
94+
${CMAKE_SOURCE_DIR}/include/libnex/decls.h
95+
${CMAKE_SOURCE_DIR}/include/libnex/endian.h
96+
${CMAKE_SOURCE_DIR}/include/libnex/list.h
97+
${CMAKE_SOURCE_DIR}/include/libnex/safestring.h
98+
${CMAKE_SOURCE_DIR}/include/libnex/bits.h
99+
${CMAKE_SOURCE_DIR}/include/object.h)
100+
101+
# Figure out which libnex.h to use
102+
if(LIBNEX_BAREMETAL)
103+
configure_file(${CMAKE_SOURCE_DIR}/src/libnex_baremetal.h ${CMAKE_BINARY_DIR}/libnex.h)
104+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
105+
${CMAKE_SOURCE_DIR}/src/libnex_baremetal.h)
106+
else()
107+
configure_file(${CMAKE_SOURCE_DIR}/src/libnex_hosted.h ${CMAKE_BINARY_DIR}/libnex.h)
108+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
109+
${CMAKE_SOURCE_DIR}/src/libnex_hosted.h)
110+
endif()
111+
set(LIBNEX_HEADER ${CMAKE_BINARY_DIR}/libnex.h)
112+
113+
# Add hosted headers
114+
list(APPEND LIBNEX_HEADERS_HOSTED
115+
${CMAKE_SOURCE_DIR}/include/libnex/getopt.h
116+
${CMAKE_SOURCE_DIR}/include/libnex/textstream.h
117+
${CMAKE_SOURCE_DIR}/include/libnex/progname.h
118+
${CMAKE_SOURCE_DIR}/include/libnex/error.h
119+
${CMAKE_SOURCE_DIR}/include/libnex/safemalloc.h)
120+
121+
# Set the headers
122+
list(APPEND LIBNEX_HEADERS ${LIBNEX_HEADERS_ALWAYS})
123+
if(NOT LIBNEX_BAREMETAL)
124+
list(APPEND LIBNEX_HEADERS ${LIBNEX_HEADERS_HOSTED})
125+
endif()
126+
127+
# Setup include directories
128+
list(APPEND LIBNEX_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}
129+
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}
130+
${NEXNIXSDK_INCLUDE_DIRS})
131+
132+
# Create the library
133+
add_library(nex ${LIBNEX_SOURCES})
134+
135+
# Set compiler flags
136+
137+
# Set SOName
138+
set_target_properties(nex PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION})
139+
# Include the directories
140+
target_include_directories(nex PRIVATE ${LIBNEX_INCLUDE_DIRS})
141+
142+
# libnex_config.h is in the binary directory for the libnex build,
143+
# but in ${CMAKE_INSTALL_INCLUDEDIR}/include/libnex for consumers. Ensure the headers differentiate
144+
# between them
145+
target_compile_definitions(nex PRIVATE "IN_LIBNEX")
146+
147+
# Ensure symbols are hidden by default
148+
if(BUILD_SHARED_LIBS)
149+
set_target_properties(nex PROPERTIES C_VISIBILITY_PRESET hidden)
150+
endif()
151+
152+
# Check for __declspec(dllexport)
153+
check_c_source_compiles([=[
154+
__declspec(dllexport) int f() {}
155+
int main() {}
156+
]=] HAVE_DECLSPEC_EXPORT)
157+
158+
if(NOT HAVE_DECLSPEC_EXPORT)
159+
# Check for __attribute__((visibility("default")))
160+
check_c_source_compiles([=[
161+
__attribute__((visibility("default"))) int f() {}
162+
int main(){}
163+
]=] HAVE_VISIBILITY)
164+
endif()
165+
166+
# Install pkgconfig script
167+
install(FILES ${CMAKE_BINARY_DIR}/nex.pc DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig)
168+
169+
# Install the library
170+
install(TARGETS nex)
171+
172+
# Figure out library type
173+
if(BUILD_SHARED_LIBS)
174+
set(LIBNEX_LIBTYPE "SHARED")
175+
set(LIBNEX_LIBRARY_FILE "${CMAKE_INSTALL_FULL_LIBDIR}/libnex.so")
176+
else()
177+
set(LIBNEX_LIBTYPE "STATIC")
178+
set(LIBNEX_LIBRARY_FILE "${CMAKE_INSTALL_FULL_LIBDIR}/libnex.a")
179+
endif()
180+
181+
# Install header files
182+
install(FILES ${LIBNEX_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnex)
183+
install(FILES ${LIBNEX_HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
184+
install(FILES ${CMAKE_BINARY_DIR}/libnex_config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnex)
185+
186+
# Configure package configuration file
187+
configure_package_config_file(${CMAKE_SOURCE_DIR}/data/LibNexConfig.cmake.in
188+
${CMAKE_BINARY_DIR}/LibNexConfig.cmake
189+
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake
190+
PATH_VARS CMAKE_INSTALL_INCLUDEDIR)
191+
192+
write_basic_package_version_file(${CMAKE_BINARY_DIR}/LibNexConfigVersion.cmake
193+
COMPATIBILITY AnyNewerVersion)
194+
195+
# Install cmake package files
196+
install(FILES ${CMAKE_BINARY_DIR}/LibNexConfig.cmake
197+
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake)
198+
install(FILES ${CMAKE_BINARY_DIR}/LibNexConfigVersion.cmake
199+
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake)
200+
201+
# Figure out which tests to run
202+
list(APPEND LIBNEX_TESTS
203+
endian
204+
list
205+
bits)
206+
207+
if(NOT HAVE_BSD_STRING)
208+
list(APPEND LIBNEX_TESTS strlcat strlcpy)
209+
endif()
210+
211+
foreach(test ${LIBNEX_TESTS})
212+
nextest_add_library_test(NAME ${test}
213+
SOURCE tests/${test}.c
214+
LIBS nex
215+
INCLUDES ${LIBNEX_INCLUDE_DIRS}
216+
DEFINES IN_LIBNEX)
217+
endforeach()

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# libnex
2+
libnex is the NexNix helper library. It contains helper functions for various mundane, yet required components of NexNix

data/LibNexConfig.cmake.in

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[[
2+
LibNexConfig.cmake.in - contains libnex package configuration parameters
3+
Copyright 2022 The NexNix Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
There should be a copy of the License distributed in a file named
8+
LICENSE, if not, you may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License
17+
]]
18+
19+
set(LIBNEX_VERSION "@PROJECT_VERSION@")
20+
set(LIBNEX_VERSION_MAJOR "@PROJECT_VERSION_MAJOR@")
21+
set(LIBNEX_VERSION_MINOR "@PROJECT_VERSION_MINOR@")
22+
set(LIBNEX_VERSION_REVISION "@PROJECT_VERSION_REVISION@")
23+
24+
@PACKAGE_INIT@
25+
26+
# Setup basic compiler flags
27+
set_and_check(LIBNEX_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
28+
set(LIBNEX_LIBRARY "@LIBNEX_LIBRARY_FILE@")
29+
30+
# Create the imported target
31+
add_library(nex @LIBNEX_LIBTYPE@ IMPORTED)
32+
set_target_properties(nex PROPERTIES IMPORTED_SONAME "@LIBNEX_LIBRARY@")
33+
34+
check_required_components(LibNex)

data/nex.pc.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
exec_prefix=@CMAKE_INSTALL_PREFIX@
3+
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
4+
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
5+
6+
Name: @PROJECT_NAME@
7+
Description: A library that wraps over common functions
8+
Version: @PROJECT_VERSION@
9+
Libs: -L${libdir} -lnex
10+
Cflags: -I${includedir}

include/libnex/bits.h

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
bits.h - contains macros to perform common bitwise operations
3+
Copyright 2022 The NexNix Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
There should be a copy of the License distributed in a file named
8+
LICENSE, if not, you may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/// @file bits.h
20+
21+
#ifndef _BITS_H
22+
#define _BITS_H
23+
24+
/**
25+
* @brief Sets a given bit in a bitset
26+
*
27+
* BitSetNew() sets a given bit in a bitset. It takes the literal number of the bit, and uses an OR operation to set it
28+
* @param set the value to set the bit in
29+
* @param bit the bit to set
30+
* @return The bitset with the set value
31+
*/
32+
#define BitSetNew(set, bit) ((set) | (1 << (bit)))
33+
34+
/**
35+
* @brief Sets a given bit in a bitset in place
36+
*
37+
* BitSet() sets a given bit in a bitset. It takes the literal number of the bit, and uses an OR operation to set it
38+
* @param set the value to set the bit in
39+
* @param bit the bit to set
40+
*/
41+
#define BitSet(set, bit) ((set) |= (1 << (bit)))
42+
43+
/**
44+
* @brief Clears a bit in a bitset
45+
*
46+
* BitClearNew() clears a given bit in a bit set. It returns a new bit set with the bit cleared
47+
* @param set the value to clear the bit it
48+
* @param bin the bit to clear
49+
* @return The set with the bit cleared
50+
*/
51+
#define BitClearNew(set, bit) ((set) & ~(1 << (bit)))
52+
53+
/**
54+
* @brief Clears a bit in a bitset
55+
*
56+
* BitClear() clears a given bit in a bit set in place
57+
* @param set the value to clear the bit it
58+
* @param bit the bit to clear
59+
*/
60+
#define BitClear(set, bit) ((set) &= ~(1 << (bit)))
61+
62+
/**
63+
* @brief Gets a bit's state in a bit set
64+
* @param set the bit set to work with
65+
* @param bit the bit to check
66+
* @return the bits's state
67+
*/
68+
#define BitGet(set, bit) (((set) & (1 << (bit))) >> (bit))
69+
70+
/**
71+
* @brief Sets a given range of bits in a bit set
72+
*
73+
* BitSetRange() accepts a range of bit sets to in a value. It sets them in place
74+
* @param set the value to set the bits in
75+
* @param start the first bit to set
76+
* @param count the number of bits to set
77+
*/
78+
#define BitSetRange(set, start, count) ((set) |= (((1 << (count)) - 1) << (start)))
79+
80+
/**
81+
* @brief Set a given range of bits in a bit set
82+
*
83+
* BitSetRangeNew() sets a range of bits in a bit set, returning a new set
84+
* @param set the the value to set bits in
85+
* @param start the first bit to set
86+
* @param count the number of bits to set
87+
* @return The new set
88+
*/
89+
#define BitSetRangeNew(set, start, count) ((set) | (((1 << (count)) - 1) << (start)))
90+
91+
/**
92+
* @brief Clears a range of bits in a bit set
93+
*
94+
* BitClearRange() clears a range of bits in a bit set in place
95+
* @param set the value to clear in
96+
* @param start the first bit to clear
97+
* @param count the number of bits to clear
98+
*/
99+
#define BitClearRange(set, start, count) ((set) &= ~(((1 << (count)) - 1) << (start)))
100+
101+
/**
102+
* @brief Clears a range of bits in a bit set
103+
*
104+
* BitClearRangeNew() clears a range of bits in a bit set, returning a new set
105+
* @param set the value to clear in
106+
* @param start the first bit to clear
107+
* @param count the number of bits to clear
108+
* @return the new bit set
109+
*/
110+
#define BitClearRangeNew(set, start, count) ((set) & ~(((1 << (count)) - 1) << (start)))
111+
112+
/**
113+
* @brief Gets a range of bits from a bit set
114+
*
115+
* BitGetRange() gets a range of bits in a bit set, returning those bits
116+
* @param set the set to get bits from
117+
* @param start the first bit to get
118+
* @param count the number of bits to get
119+
* @return The new set
120+
*/
121+
#define BitGetRange(set, start, count) (((set) >> (start)) & ((1 << (count)) - 1))
122+
123+
#endif

0 commit comments

Comments
 (0)