This commit is contained in:
parent
9a67a561a5
commit
10e26e8a6c
117
CLAUDE.md
Normal file
117
CLAUDE.md
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Project Overview
|
||||||
|
|
||||||
|
This repository contains a Docker-based build system for creating statically-linked C++ executables using Alpine Linux and musl libc. It consists of:
|
||||||
|
|
||||||
|
1. **dropshell-build-base**: A comprehensive C++ development base image with Drogon web framework and database support
|
||||||
|
2. **dropshell-build**: A multi-stage Docker build system that uses the base image to compile C++ projects
|
||||||
|
3. **ipdemo**: A sample application demonstrating the build system
|
||||||
|
|
||||||
|
## Key Commands
|
||||||
|
|
||||||
|
### Building the Base Image
|
||||||
|
```bash
|
||||||
|
cd build-base
|
||||||
|
./build.sh
|
||||||
|
```
|
||||||
|
This creates the `gitea.jde.nz/public/dropshell-build-base:latest` Docker image with all dependencies.
|
||||||
|
|
||||||
|
### Building a Project
|
||||||
|
```bash
|
||||||
|
./build.sh
|
||||||
|
```
|
||||||
|
This builds the project defined in `build.sh` (default: ipdemo) and outputs the binary to `output/`.
|
||||||
|
|
||||||
|
### Testing the Built Binary
|
||||||
|
```bash
|
||||||
|
./test.sh
|
||||||
|
```
|
||||||
|
Runs the compiled binary from `output/` directory.
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
- `CMAKE_BUILD_TYPE`: Set to "Debug" or "Release" (default: Debug)
|
||||||
|
- `PROJECT`: The project name to build (default: ipdemo)
|
||||||
|
|
||||||
|
## Architecture and Design
|
||||||
|
|
||||||
|
### Build System Structure
|
||||||
|
|
||||||
|
The build system uses a two-stage approach:
|
||||||
|
|
||||||
|
1. **Base Image** (`dropshell-build-base`): Contains all dependencies compiled from source with static linking:
|
||||||
|
- C++ compiler toolchain with ccache and mold linker
|
||||||
|
- Libraries: OpenSSL, jsoncpp, PostgreSQL, MariaDB, MySQL, SQLite3, cURL, c-ares
|
||||||
|
- Drogon web framework with full database support
|
||||||
|
- All libraries built with `-fPIC` for static linking
|
||||||
|
|
||||||
|
2. **Project Build** (`Dockerfile.dropshell-build`): Multi-stage build that:
|
||||||
|
- Uses build cache mounts for faster rebuilds
|
||||||
|
- Runs cmake_prebuild.sh for custom pre-build steps
|
||||||
|
- Generates version information dynamically
|
||||||
|
- Produces a single static binary in a scratch container
|
||||||
|
|
||||||
|
### CMake Configuration
|
||||||
|
|
||||||
|
The CMakeLists.txt enforces static linking through:
|
||||||
|
- `CMAKE_EXE_LINKER_FLAGS` with `-static` flag
|
||||||
|
- `CMAKE_FIND_LIBRARY_SUFFIXES` set to `.a`
|
||||||
|
- `BUILD_SHARED_LIBS` forced to OFF
|
||||||
|
- Custom library paths from the base image
|
||||||
|
|
||||||
|
Important: Projects must set CMAKE_PREFIX_PATH and explicitly link PostgreSQL libraries:
|
||||||
|
```cmake
|
||||||
|
# Set paths for libraries before finding Drogon
|
||||||
|
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}
|
||||||
|
/usr/local/jsoncpp
|
||||||
|
/usr/local/openssl-musl
|
||||||
|
/usr/local/pgsql
|
||||||
|
# ... other library paths
|
||||||
|
)
|
||||||
|
|
||||||
|
# Additional PostgreSQL libraries needed for static linking
|
||||||
|
set(POSTGRESQL_EXTRA_LIBS
|
||||||
|
/usr/local/pgsql/lib/libpgcommon.a
|
||||||
|
/usr/local/pgsql/lib/libpgport.a
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Project Structure for New Applications
|
||||||
|
|
||||||
|
New C++ projects should follow this structure:
|
||||||
|
```
|
||||||
|
project-name/
|
||||||
|
├── CMakeLists.txt # Based on ipdemo example
|
||||||
|
├── cmake_prebuild.sh # Optional pre-build script
|
||||||
|
└── src/
|
||||||
|
├── main.cpp # Application entry point
|
||||||
|
└── version.hpp.in # Version template
|
||||||
|
```
|
||||||
|
|
||||||
|
### Adding New Projects
|
||||||
|
|
||||||
|
To build a different project:
|
||||||
|
1. Create a directory with the project structure above
|
||||||
|
2. Modify `build.sh` to set `PROJECT="your-project-name"`
|
||||||
|
3. Run `./build.sh` to build
|
||||||
|
|
||||||
|
### Static Linking Details
|
||||||
|
|
||||||
|
The system ensures complete static linking by:
|
||||||
|
- Using Alpine Linux's musl libc
|
||||||
|
- Building all dependencies with `--enable-static --disable-shared`
|
||||||
|
- Setting `-fPIC` for position-independent code
|
||||||
|
- Using mold linker with `-static` flag
|
||||||
|
- Configuring CMake to prefer `.a` libraries
|
||||||
|
|
||||||
|
### Database Support
|
||||||
|
|
||||||
|
Applications can use:
|
||||||
|
- PostgreSQL via libpq at `/usr/local/pgsql/`
|
||||||
|
- MySQL via client library at `/usr/local/mysql/`
|
||||||
|
- MariaDB via connector at `/usr/local/mariadb-connector-c/`
|
||||||
|
- SQLite3 at `/usr/local/sqlite3/`
|
||||||
|
|
||||||
|
All database libraries are statically linked into the final binary.
|
@ -96,6 +96,22 @@ set(EXTRA_LIBS
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Set paths for libraries before finding Drogon
|
||||||
|
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}
|
||||||
|
/usr/local/jsoncpp
|
||||||
|
/usr/local/openssl-musl
|
||||||
|
/usr/local/pgsql
|
||||||
|
/usr/local/mariadb-connector-c
|
||||||
|
/usr/local/sqlite3
|
||||||
|
/usr/local/mysql
|
||||||
|
/usr/local/cares
|
||||||
|
/usr/local/curl
|
||||||
|
)
|
||||||
|
|
||||||
|
# Explicitly set jsoncpp paths for Drogon's FindJsoncpp.cmake
|
||||||
|
set(JSONCPP_INCLUDE_DIRS /usr/local/jsoncpp/include)
|
||||||
|
set(JSONCPP_LIBRARIES /usr/local/jsoncpp/lib/libjsoncpp.a)
|
||||||
|
|
||||||
##########
|
##########
|
||||||
# If you include the drogon source code locally in your project, use this method to add drogon
|
# If you include the drogon source code locally in your project, use this method to add drogon
|
||||||
# add_subdirectory(external/drogon)
|
# add_subdirectory(external/drogon)
|
||||||
@ -105,10 +121,17 @@ find_package(Drogon CONFIG REQUIRED)
|
|||||||
target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon)
|
target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon)
|
||||||
|
|
||||||
|
|
||||||
|
# Additional PostgreSQL libraries needed for static linking
|
||||||
|
set(POSTGRESQL_EXTRA_LIBS
|
||||||
|
/usr/local/pgsql/lib/libpgcommon.a
|
||||||
|
/usr/local/pgsql/lib/libpgport.a
|
||||||
|
)
|
||||||
|
|
||||||
# Link libraries
|
# Link libraries
|
||||||
target_link_libraries(${PROJECT_EXE_NAME} PRIVATE
|
target_link_libraries(${PROJECT_EXE_NAME} PRIVATE
|
||||||
nlohmann_json::nlohmann_json
|
nlohmann_json::nlohmann_json
|
||||||
Drogon::Drogon
|
Drogon::Drogon
|
||||||
|
${POSTGRESQL_EXTRA_LIBS}
|
||||||
${EXTRA_LIBS}
|
${EXTRA_LIBS}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include <drogon/HttpClient.h>
|
#include <drogon/drogon.h>
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "assert.hpp"
|
#include "assert.hpp"
|
||||||
|
|
||||||
@ -14,24 +17,36 @@ int main() {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << "Retrieving IP address..." << std::endl;
|
std::cout << "Retrieving IP address..." << std::endl;
|
||||||
|
|
||||||
auto loop = trantor::EventLoop::getEventLoopOfCurrentThread();
|
// Initialize drogon app but don't run it
|
||||||
auto client = drogon::HttpClient::newHttpClient("http://ipinfo.io", loop);
|
drogon::app().setLogLevel(trantor::Logger::kError);
|
||||||
auto req = drogon::HttpRequest::newHttpRequest();
|
|
||||||
req->setMethod(drogon::Get);
|
|
||||||
req->setPath("/ip");
|
|
||||||
std::string ip_body;
|
std::string ip_body;
|
||||||
int status = 0;
|
int status = 0;
|
||||||
bool done = false;
|
bool done = false;
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr &resp) {
|
|
||||||
if (result == drogon::ReqResult::Ok && resp) {
|
// Run in a separate thread to avoid event loop conflicts
|
||||||
ip_body = resp->body();
|
std::thread worker([&]() {
|
||||||
status = resp->statusCode();
|
trantor::EventLoop loop;
|
||||||
} else {
|
auto client = drogon::HttpClient::newHttpClient("http://ipinfo.io", &loop);
|
||||||
status = 0;
|
auto req = drogon::HttpRequest::newHttpRequest();
|
||||||
}
|
req->setMethod(drogon::Get);
|
||||||
done = true;
|
req->setPath("/ip");
|
||||||
|
|
||||||
|
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr &resp) {
|
||||||
|
if (result == drogon::ReqResult::Ok && resp) {
|
||||||
|
ip_body = resp->body();
|
||||||
|
status = resp->statusCode();
|
||||||
|
} else {
|
||||||
|
status = 0;
|
||||||
|
}
|
||||||
|
done = true;
|
||||||
|
loop.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
loop.loop();
|
||||||
});
|
});
|
||||||
while (!done) loop->loopOnce();
|
|
||||||
|
worker.join();
|
||||||
|
|
||||||
ASSERT(status == 200, "Failed to get IP");
|
ASSERT(status == 200, "Failed to get IP");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user