test: Update 5 files
This commit is contained in:
@ -32,3 +32,6 @@ build_project "ipdemo"
|
||||
echo "Building test_libs..."
|
||||
build_project "test_libs"
|
||||
|
||||
echo "Building cprdemo..."
|
||||
build_project "cprdemo"
|
||||
|
||||
|
63
tests/cprdemo/CMakeLists.txt
Normal file
63
tests/cprdemo/CMakeLists.txt
Normal file
@ -0,0 +1,63 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
# Project setup
|
||||
if(NOT DEFINED PROJECT_NAME)
|
||||
message(FATAL_ERROR "PROJECT_NAME is not defined. Pass it via -DPROJECT_NAME=<name>")
|
||||
endif()
|
||||
|
||||
string(TIMESTAMP PROJECT_VERSION "%Y.%m%d.%H%M")
|
||||
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)
|
||||
|
||||
# Build configuration
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static")
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
set(CMAKE_PREFIX_PATH /usr/local)
|
||||
|
||||
# Create executable
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
# Configure version.hpp
|
||||
configure_file("src/version.hpp.in" "src/autogen/version.hpp" @ONLY)
|
||||
|
||||
# Pre-build script
|
||||
add_custom_target(run_prebuild_script ALL
|
||||
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake_prebuild.sh
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_dependencies(${PROJECT_NAME} run_prebuild_script)
|
||||
|
||||
# Include directories
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_BINARY_DIR}/src/autogen
|
||||
src)
|
||||
|
||||
# Find packages
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(nlohmann_json REQUIRED)
|
||||
|
||||
# Find CURL manually since we built it without CMake targets
|
||||
find_library(CURL_LIBRARY NAMES curl libcurl PATHS /usr/local/lib NO_DEFAULT_PATH)
|
||||
find_path(CURL_INCLUDE_DIR NAMES curl/curl.h PATHS /usr/local/include NO_DEFAULT_PATH)
|
||||
|
||||
# Create CURL target if not found
|
||||
if(NOT TARGET CURL::libcurl)
|
||||
add_library(CURL::libcurl STATIC IMPORTED)
|
||||
set_target_properties(CURL::libcurl PROPERTIES
|
||||
IMPORTED_LOCATION ${CURL_LIBRARY}
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CURL_INCLUDE_DIR}
|
||||
INTERFACE_LINK_LIBRARIES "/usr/lib/libz.a;/usr/lib/libzstd.a"
|
||||
)
|
||||
endif()
|
||||
|
||||
# Find CPR after CURL target is available
|
||||
find_package(cpr REQUIRED)
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
nlohmann_json::nlohmann_json
|
||||
cpr::cpr
|
||||
lzma dl)
|
8
tests/cprdemo/cmake_prebuild.sh
Executable file
8
tests/cprdemo/cmake_prebuild.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "CPR Demo Pre-build Script"
|
||||
echo "========================="
|
||||
echo "Build time: $(date)"
|
||||
echo "Working directory: $(pwd)"
|
||||
echo "CMAKE args: $@"
|
||||
echo "CPR Demo ready for build"
|
13
tests/cprdemo/src/assert.hpp
Normal file
13
tests/cprdemo/src/assert.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#define ASSERT(condition, message) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
std::cerr << "Assertion failed: " << message << std::endl; \
|
||||
std::cerr << "File: " << __FILE__ << ", Line: " << __LINE__ << std::endl; \
|
||||
throw std::runtime_error(message); \
|
||||
} \
|
||||
} while (false)
|
33
tests/cprdemo/src/main.cpp
Normal file
33
tests/cprdemo/src/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cpr/cpr.h>
|
||||
|
||||
#include "version.hpp"
|
||||
#include "assert.hpp"
|
||||
|
||||
void crashy() {
|
||||
ASSERT(false, "SUCCESS!");
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "cprdemo version: " << cprdemo::VERSION << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Retrieving IP address using CPR..." << std::endl;
|
||||
|
||||
// Use CPR to make HTTP GET request
|
||||
cpr::Response r = cpr::Get(cpr::Url{"https://ipinfo.io/ip"});
|
||||
|
||||
ASSERT(r.status_code == 200, "Failed to get IP");
|
||||
|
||||
nlohmann::json j;
|
||||
j["ip"] = r.text;
|
||||
j["status"] = r.status_code;
|
||||
|
||||
std::cout << j.dump(4) << std::endl;
|
||||
|
||||
std::cout << "Done" << std::endl;
|
||||
|
||||
crashy();
|
||||
|
||||
return 0;
|
||||
}
|
5
tests/cprdemo/src/version.hpp.in
Normal file
5
tests/cprdemo/src/version.hpp.in
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace @PROJECT_NAME@ {
|
||||
constexpr const char* VERSION = "@PROJECT_VERSION@";
|
||||
}
|
@ -28,3 +28,13 @@ else
|
||||
echo "ipdemo binary not found - run ./build.sh first"
|
||||
fi
|
||||
|
||||
# Test cprdemo
|
||||
if [ -f "${SCRIPT_DIR}/output/cprdemo" ]; then
|
||||
echo ""
|
||||
echo "Testing cprdemo..."
|
||||
echo "Running cprdemo..."
|
||||
# Run cprdemo and capture both stdout and stderr, ignoring the crash
|
||||
"${SCRIPT_DIR}/output/cprdemo" 2>&1 || true
|
||||
echo "cprdemo test completed!"
|
||||
fi
|
||||
|
||||
|
@ -1,12 +1,29 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(test_libs)
|
||||
|
||||
# Project setup
|
||||
if(NOT DEFINED PROJECT_NAME)
|
||||
message(FATAL_ERROR "PROJECT_NAME is not defined. Pass it via -DPROJECT_NAME=<name>")
|
||||
endif()
|
||||
|
||||
string(TIMESTAMP PROJECT_VERSION "%Y.%m%d.%H%M")
|
||||
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)
|
||||
|
||||
# Build configuration
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Force static linking
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static")
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
set(CMAKE_PREFIX_PATH /usr/local)
|
||||
|
||||
# Create executable from the cpp file in root (special case for test_libs)
|
||||
add_executable(${PROJECT_NAME} test_libs.cpp)
|
||||
|
||||
# Pre-build script
|
||||
add_custom_target(run_prebuild_script ALL
|
||||
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake_prebuild.sh
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_dependencies(${PROJECT_NAME} run_prebuild_script)
|
||||
|
||||
# Find packages
|
||||
find_package(PkgConfig REQUIRED)
|
||||
@ -17,17 +34,8 @@ find_package(Threads REQUIRED)
|
||||
# Find SQLite3 using pkg-config as fallback
|
||||
pkg_check_modules(SQLite3 REQUIRED sqlite3)
|
||||
|
||||
# Add custom target to run cmake_prebuild.sh at the start of the build process
|
||||
add_custom_target(run_prebuild_script ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Running cmake_prebuild.sh..."
|
||||
COMMAND ${CMAKE_COMMAND} -E env bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake_prebuild.sh
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
|
||||
add_executable(test_libs test_libs.cpp)
|
||||
|
||||
target_link_libraries(test_libs
|
||||
# Link libraries
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
fmt::fmt
|
||||
spdlog::spdlog
|
||||
${SQLite3_STATIC_LIBRARIES}
|
||||
@ -35,5 +43,5 @@ target_link_libraries(test_libs
|
||||
rt
|
||||
)
|
||||
|
||||
target_include_directories(test_libs PRIVATE ${SQLite3_INCLUDE_DIRS})
|
||||
target_compile_options(test_libs PRIVATE ${SQLite3_CFLAGS_OTHER})
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${SQLite3_INCLUDE_DIRS})
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE ${SQLite3_CFLAGS_OTHER})
|
Reference in New Issue
Block a user