Skip to content

Commit fb28bbf

Browse files
authored
Merge pull request #78 from ahmedyarub/ay/refactor_cmake_scripts
Add examples, and pre and post CMake building scripts
2 parents 3cb0df0 + 9a1e927 commit fb28bbf

File tree

9 files changed

+102
-55
lines changed

9 files changed

+102
-55
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,33 @@ jobs:
1515
- name: Prepare
1616
run: |
1717
sudo apt-get update
18-
sudo apt-get install -y libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
18+
sudo apt-get install -y libssl-dev libcurl4-openssl-dev uncrustify
1919
- name: Prepare libwebsockets
2020
run: |
2121
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
2222
cd libwebsockets
2323
mkdir build
2424
cd build
25-
cmake ..
26-
make
25+
cmake .. -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON-DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
26+
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic"
27+
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
28+
sudo make install
29+
- name: Prepare libyaml
30+
run: |
31+
git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
32+
cd libyaml
33+
mkdir build
34+
cd build
35+
cmake .. -DBUILD_TESTING=OFF
36+
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
2737
sudo make install
2838
- name: Build client library
2939
run: |
3040
cd kubernetes
3141
mkdir build
3242
cd build
3343
cmake ..
34-
make
44+
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
3545
- name: Build authentication plugin - oidc
3646
run: |
3747
cd kubernetes/config/authn_plugin/plugins/oidc

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ git clone https://github.com/kubernetes-client/c
1414
CLIENT_REPO_ROOT=${PWD}/c
1515

1616
# Install pre-requisites
17-
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
17+
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify
1818

1919
# Build pre-requisite: libwebsockets
2020
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
2121
cd libwebsockets
2222
mkdir build
2323
cd build
24-
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
24+
cmake -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON-DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
25+
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic" -DCMAKE_INSTALL_PREFIX=/usr/local ..
26+
make
27+
sudo make install
28+
29+
# Build pre-requisite: libyaml
30+
git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
31+
cd libyaml
32+
mkdir build
33+
cd build
34+
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTING=OFF ..
2535
make
2636
sudo make install
2737

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(pkgName "kubernetes")
2+
3+
add_subdirectory(list_pod)

examples/list_pod/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(list_pod main.c)
2+
3+
find_package(${pkgName})
4+
5+
target_link_libraries(list_pod PRIVATE ${pkgName})

examples/list_pod/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <kube_config.h>
22
#include <apiClient.h>
33
#include <CoreV1API.h>
4-
#include <malloc.h>
54
#include <stdio.h>
6-
#include <errno.h>
75

86
void list_pod(apiClient_t * apiClient)
97
{
@@ -35,7 +33,7 @@ void list_pod(apiClient_t * apiClient)
3533
}
3634
}
3735

38-
int main(int argc, char *argv[])
36+
int main()
3937
{
4038
char *basePath = NULL;
4139
sslConfig_t *sslConfig = NULL;

kubernetes/CMakeLists.txt

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
1-
cmake_minimum_required (VERSION 2.6)
1+
cmake_minimum_required (VERSION 2.6...3.10.2)
22
project (CGenerator)
33

44
cmake_policy(SET CMP0063 NEW)
55

66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_CXX_VISIBILITY_PRESET default)
88
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
9-
set(CMAKE_BUILD_TYPE Debug)
9+
10+
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1011

1112
find_package(OpenSSL)
1213

1314
if (OPENSSL_FOUND)
14-
message (STATUS "OPENSSL found")
15-
set (CMAKE_C_FLAGS "-DOPENSSL")
16-
include_directories(${OPENSSL_INCLUDE_DIR})
17-
include_directories(${OPENSSL_INCLUDE_DIRS})
18-
link_directories(${OPENSSL_LIBRARIES})
19-
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
20-
else()
21-
message (STATUS "OpenSSL Not found.")
15+
message (STATUS "OPENSSL found")
16+
17+
set(CMAKE_C_FLAGS "-DOPENSSL")
18+
if(CMAKE_VERSION VERSION_LESS 3.4)
19+
include_directories(${OPENSSL_INCLUDE_DIR})
20+
include_directories(${OPENSSL_INCLUDE_DIRS})
21+
link_directories(${OPENSSL_LIBRARIES})
22+
endif()
23+
24+
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
25+
else()
26+
message (STATUS "OpenSSL Not found.")
2227
endif()
2328

2429
set(pkgName "kubernetes")
2530

2631
find_package(CURL 7.58.0 REQUIRED)
2732
if(CURL_FOUND)
28-
include_directories(${CURL_INCLUDE_DIR})
29-
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
33+
include_directories(${CURL_INCLUDE_DIR})
34+
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
3035
else(CURL_FOUND)
31-
message(FATAL_ERROR "Could not find the CURL library and development files.")
36+
message(FATAL_ERROR "Could not find the CURL library and development files.")
3237
endif()
3338

3439
set(SRCS
35-
config/kube_config_model.c
36-
config/kube_config_yaml.c
37-
config/kube_config_util.c
38-
config/kube_config.c
39-
config/incluster_config.c
40-
config/exec_provider.c
41-
config/authn_plugin/authn_plugin_util.c
42-
config/authn_plugin/authn_plugin.c
43-
watch/watch_util.c
44-
websocket/wsclient.c
45-
websocket/kube_exec.c
4640
src/list.c
4741
src/apiKey.c
4842
src/apiClient.c
4943
src/binary.c
50-
src/generic.c
5144
external/cJSON.c
5245
model/object.c
5346
model/admissionregistration_v1_service_reference.c
@@ -796,23 +789,10 @@ set(SRCS
796789
)
797790

798791
set(HDRS
799-
config/kube_config_common.h
800-
config/kube_config_model.h
801-
config/kube_config_yaml.h
802-
config/kube_config_util.h
803-
config/kube_config.h
804-
config/incluster_config.h
805-
config/exec_provider.h
806-
config/authn_plugin/authn_plugin_util.h
807-
config/authn_plugin/authn_plugin.h
808-
watch/watch_util.h
809-
websocket/wsclient.h
810-
websocket/kube_exec.h
811792
include/apiClient.h
812793
include/list.h
813794
include/binary.h
814795
include/keyValuePair.h
815-
include/generic.h
816796
external/cJSON.h
817797
model/object.h
818798
model/admissionregistration_v1_service_reference.h
@@ -1560,10 +1540,18 @@ set(HDRS
15601540

15611541
)
15621542

1563-
# Add library with project file with projectname as library name
1564-
add_library(${pkgName} SHARED ${SRCS} ${HDRS})
1543+
include(PreTarget.cmake OPTIONAL)
1544+
1545+
# Add library with project file with project name as library name
1546+
add_library(${pkgName} ${SRCS} ${HDRS})
15651547
# Link dependent libraries
1548+
if(NOT CMAKE_VERSION VERSION_LESS 3.4)
1549+
target_link_libraries(${pkgName} OpenSSL::SSL OpenSSL::Crypto)
1550+
endif()
15661551
target_link_libraries(${pkgName} ${CURL_LIBRARIES} )
1552+
1553+
include(PostTarget.cmake OPTIONAL)
1554+
15671555
#install library to destination
15681556
install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX})
15691557

@@ -1572,7 +1560,7 @@ set(SRCS "")
15721560
set(HDRS "")
15731561

15741562

1575-
## This section shows how to use the above compiled libary to compile the source files
1563+
## This section shows how to use the above compiled library to compile the source files
15761564
## set source files
15771565
#set(SRCS
15781566
# unit-tests/manual-AdmissionregistrationAPI.c
@@ -1653,12 +1641,12 @@ set(HDRS "")
16531641

16541642
## loop over all files in SRCS variable
16551643
#foreach(SOURCE_FILE ${SRCS})
1656-
# # Get only the file name from the file as add_executable doesnot support executable with slash("/")
1644+
# # Get only the file name from the file as add_executable does not support executable with slash("/")
16571645
# get_filename_component(FILE_NAME_ONLY ${SOURCE_FILE} NAME_WE)
16581646
# # Remove .c from the file name and set it as executable name
16591647
# string( REPLACE ".c" "" EXECUTABLE_FILE ${FILE_NAME_ONLY})
16601648
# # Add executable for every source file in SRCS
16611649
# add_executable(unit-${EXECUTABLE_FILE} ${SOURCE_FILE})
1662-
# # Link above created libary to executable and dependent libary curl
1650+
# # Link above created library to executable and dependent library curl
16631651
# target_link_libraries(unit-${EXECUTABLE_FILE} ${CURL_LIBRARIES} ${pkgName} )
16641652
#endforeach(SOURCE_FILE ${SRCS})

kubernetes/PostTarget.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target_link_libraries(${pkgName} yaml websockets)
2+
set_target_properties(${pkgName} PROPERTIES LINKER_LANGUAGE C)

kubernetes/PreTarget.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
list(APPEND SRCS
2+
config/kube_config_model.c
3+
config/kube_config_yaml.c
4+
config/kube_config_util.c
5+
config/kube_config.c
6+
config/incluster_config.c
7+
config/exec_provider.c
8+
config/authn_plugin/authn_plugin_util.c
9+
config/authn_plugin/authn_plugin.c
10+
watch/watch_util.c
11+
websocket/wsclient.c
12+
websocket/kube_exec.c
13+
src/generic.c)
14+
15+
list(APPEND HDRS
16+
config/kube_config_common.h
17+
config/kube_config_model.h
18+
config/kube_config_yaml.h
19+
config/kube_config_util.h
20+
config/kube_config.h
21+
config/incluster_config.h
22+
config/exec_provider.h
23+
config/authn_plugin/authn_plugin_util.h
24+
config/authn_plugin/authn_plugin.h
25+
watch/watch_util.h
26+
websocket/wsclient.h
27+
websocket/kube_exec.h
28+
include/generic.h)
29+
30+
find_package(libwebsockets REQUIRED)
31+
find_package(yaml CONFIG REQUIRED)

kubernetes/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This will compile the generated code and create a library in the build folder wh
3939
mkdir build
4040
cd build
4141
// To install library to specific location, use following commands
42-
cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton ..
42+
cmake -DCMAKE_INSTALL_PREFIX=/pathtolocation ..
4343
// for normal install use following command
4444
cmake ..
4545
make
@@ -49,14 +49,14 @@ sudo make install
4949
Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined and created)
5050

5151
To compile main.c(considering the file is present in build folder) use following command
52-
-L - locaiton of the library(not required if cmake with normal installation is performed)
52+
-L - location of the library(not required if cmake with normal installation is performed)
5353
-l library name
5454
```bash
5555
gcc main.c -L. -lkubernetes -o main
5656
```
5757
Once compiled, you can run it with ``` ./main ```
5858

59-
Note: You don't need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work.
59+
Note: You don't need to specify includes for models and include folder separately as they are path linked. You just have to import the api.h file in your code, the include linking will work.
6060

6161
## Documentation for API Endpoints
6262

0 commit comments

Comments
 (0)