From c147eae84d55c6674b745ac88cb58a01fbfad988 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Tue, 15 Jul 2025 23:56:39 +0200 Subject: [PATCH 01/13] First localdev support for macos --- build/osx-localdev.sh | 159 +++++++++++++++++++++ sdks/rust/test-optimization-sdk/Cargo.lock | 2 +- 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100755 build/osx-localdev.sh diff --git a/build/osx-localdev.sh b/build/osx-localdev.sh new file mode 100755 index 0000000..53826cc --- /dev/null +++ b/build/osx-localdev.sh @@ -0,0 +1,159 @@ +#!/bin/bash + +set -e # Exit on any error + +# Configuration +DEV_DIR="../dev" +REPO_URL="https://github.com/DataDog/dd-trace-go.git" +BRANCH="main" +TARGET_DIR="$DEV_DIR/internal/civisibility/native" + +# Colors for output +GREEN='\033[0;32m' +BLUE='\033[0;34m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +echo -e "${BLUE}=== Test Optimization Native Library - macOS Local Development Build ===${NC}" + +# Check if Go is installed +if ! command -v go &> /dev/null; then + echo -e "${RED}Error: Go is not installed. Please install Go 1.24 or later.${NC}" + exit 1 +fi + +# Check Go version +GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//') +echo -e "${GREEN}✓ Found Go version: $GO_VERSION${NC}" + +# Check if brew and coreutils are available +if ! command -v brew &> /dev/null; then + echo -e "${YELLOW}Warning: Homebrew not found. You may need to install coreutils manually.${NC}" +elif ! brew list coreutils &> /dev/null; then + echo -e "${YELLOW}Installing coreutils...${NC}" + brew install coreutils +fi + +# Function to check if directory is a git repository +is_git_repo() { + [ -d "$1/.git" ] +} + +# Function to get current branch +get_current_branch() { + git -C "$1" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "" +} + +# Function to check if repo is clean +is_repo_clean() { + [ -z "$(git -C "$1" status --porcelain)" ] +} + +echo -e "${BLUE}--- Setting up dd-trace-go repository ---${NC}" + +if [ -d "$DEV_DIR" ]; then + if is_git_repo "$DEV_DIR"; then + echo -e "${GREEN}✓ Found existing dd-trace-go repository in $DEV_DIR${NC}" + + # Check current branch + CURRENT_BRANCH=$(get_current_branch "$DEV_DIR") + if [ "$CURRENT_BRANCH" != "$BRANCH" ]; then + echo -e "${YELLOW}Current branch is '$CURRENT_BRANCH', switching to '$BRANCH'...${NC}" + git -C "$DEV_DIR" fetch origin + git -C "$DEV_DIR" checkout "$BRANCH" + fi + + # Pull latest changes + echo -e "${YELLOW}Pulling latest changes from $BRANCH...${NC}" + git -C "$DEV_DIR" pull origin "$BRANCH" + else + echo -e "${RED}Error: $DEV_DIR exists but is not a git repository${NC}" + echo -e "${YELLOW}Please remove $DEV_DIR directory and run this script again${NC}" + exit 1 + fi +else + echo -e "${YELLOW}Cloning dd-trace-go repository...${NC}" + git clone --branch "$BRANCH" "$REPO_URL" "$DEV_DIR" + echo -e "${GREEN}✓ Repository cloned successfully${NC}" +fi + +echo -e "${BLUE}--- Preparing build environment ---${NC}" + +# Create target directory +echo -e "${YELLOW}Creating target directory: $TARGET_DIR${NC}" +mkdir -p "$TARGET_DIR" + +# Copy build files +echo -e "${YELLOW}Copying build files...${NC}" +cp -rf ./* "$TARGET_DIR/" +echo -e "${GREEN}✓ Build files copied${NC}" + +# Copy native files +echo -e "${YELLOW}Copying native files...${NC}" +cp -rf ../native/* "$TARGET_DIR/" +echo -e "${GREEN}✓ Native files copied${NC}" + +echo -e "${BLUE}--- Building for macOS ---${NC}" + +# Change to target directory +cd "$TARGET_DIR" + +# Create output directory +mkdir -p ./output + +# Set build environment variables +export CGO_CFLAGS="-mmacosx-version-min=11.0 -O2 -Os -DNDEBUG -fdata-sections -ffunction-sections" +export CGO_LDFLAGS="-Wl,-x" +export GOOS=darwin +export CGO_ENABLED=1 + +echo -e "${YELLOW}Building ARM64 static library...${NC}" +GOARCH=arm64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-arm64-libtestoptimization-static/libtestoptimization.a *.go +echo -e "${GREEN}✓ ARM64 static library built${NC}" + +echo -e "${YELLOW}Building ARM64 dynamic library...${NC}" +GOARCH=arm64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib *.go +strip -x ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib +echo -e "${GREEN}✓ ARM64 dynamic library built${NC}" + +echo -e "${YELLOW}Building AMD64 static library...${NC}" +GOARCH=amd64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-x64-libtestoptimization-static/libtestoptimization.a *.go +echo -e "${GREEN}✓ AMD64 static library built${NC}" + +echo -e "${YELLOW}Building AMD64 dynamic library...${NC}" +GOARCH=amd64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib *.go +strip -x ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib +echo -e "${GREEN}✓ AMD64 dynamic library built${NC}" + +echo -e "${YELLOW}Creating universal binaries...${NC}" + +# Create universal static library +mkdir -p ./output/macos-libtestoptimization-static +lipo -create ./output/macos-arm64-libtestoptimization-static/libtestoptimization.a ./output/macos-x64-libtestoptimization-static/libtestoptimization.a -output ./output/macos-libtestoptimization-static/libtestoptimization.a +cp ./output/macos-arm64-libtestoptimization-static/libtestoptimization.h ./output/macos-libtestoptimization-static/libtestoptimization.h + +# Create universal dynamic library +mkdir -p ./output/macos-libtestoptimization-dynamic +lipo -create ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib -output ./output/macos-libtestoptimization-dynamic/libtestoptimization.dylib +cp ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h ./output/macos-libtestoptimization-dynamic/libtestoptimization.h + +echo -e "${GREEN}✓ Universal binaries created${NC}" + +echo -e "${YELLOW}Building for iOS...${NC}" +GOOS=ios GOARCH=arm64 CGO_ENABLED=1 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/ios-libtestoptimization-static/libtestoptimization.a *.go +echo -e "${GREEN}✓ iOS library built${NC}" + +echo -e "${BLUE}--- Build Summary ---${NC}" +echo -e "${GREEN}✓ All libraries built successfully!${NC}" +echo -e "${YELLOW}Output directory: $(pwd)/output${NC}" +echo -e "${YELLOW}Available builds:${NC}" +echo " • macOS Universal Static: ./output/macos-libtestoptimization-static/" +echo " • macOS Universal Dynamic: ./output/macos-libtestoptimization-dynamic/" +echo " • macOS ARM64 Static: ./output/macos-arm64-libtestoptimization-static/" +echo " • macOS ARM64 Dynamic: ./output/macos-arm64-libtestoptimization-dynamic/" +echo " • macOS AMD64 Static: ./output/macos-x64-libtestoptimization-static/" +echo " • macOS AMD64 Dynamic: ./output/macos-x64-libtestoptimization-dynamic/" +echo " • iOS Static: ./output/ios-libtestoptimization-static/" + +echo -e "${BLUE}=== Build completed successfully! ===${NC}" \ No newline at end of file diff --git a/sdks/rust/test-optimization-sdk/Cargo.lock b/sdks/rust/test-optimization-sdk/Cargo.lock index f3bfdb7..a22c29e 100644 --- a/sdks/rust/test-optimization-sdk/Cargo.lock +++ b/sdks/rust/test-optimization-sdk/Cargo.lock @@ -322,7 +322,7 @@ dependencies = [ [[package]] name = "test-optimization-sdk" -version = "0.0.2" +version = "0.0.3" dependencies = [ "cc", "rustc_version_runtime", From 74d0683a615c8b09cef7e08e98f1a7a977f9c3d3 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:06:39 +0200 Subject: [PATCH 02/13] improve the osx-localdev script --- .gitignore | 4 ++- build/osx-localdev.sh | 57 +++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 3d72576..a3ee527 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .DS_Store -.idea \ No newline at end of file +.idea +dev/ +dev-output/ \ No newline at end of file diff --git a/build/osx-localdev.sh b/build/osx-localdev.sh index 53826cc..9c7e2cd 100755 --- a/build/osx-localdev.sh +++ b/build/osx-localdev.sh @@ -4,6 +4,7 @@ set -e # Exit on any error # Configuration DEV_DIR="../dev" +OUTPUT_DIR="../dev-output" REPO_URL="https://github.com/DataDog/dd-trace-go.git" BRANCH="main" TARGET_DIR="$DEV_DIR/internal/civisibility/native" @@ -96,12 +97,16 @@ echo -e "${GREEN}✓ Native files copied${NC}" echo -e "${BLUE}--- Building for macOS ---${NC}" +# Create output directory at repo root +echo -e "${YELLOW}Creating output directory: $OUTPUT_DIR${NC}" +mkdir -p "$OUTPUT_DIR" + +# Get absolute path to output directory before changing directories +ABS_OUTPUT_DIR=$(realpath "$OUTPUT_DIR") + # Change to target directory cd "$TARGET_DIR" -# Create output directory -mkdir -p ./output - # Set build environment variables export CGO_CFLAGS="-mmacosx-version-min=11.0 -O2 -Os -DNDEBUG -fdata-sections -ffunction-sections" export CGO_LDFLAGS="-Wl,-x" @@ -109,51 +114,55 @@ export GOOS=darwin export CGO_ENABLED=1 echo -e "${YELLOW}Building ARM64 static library...${NC}" -GOARCH=arm64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-arm64-libtestoptimization-static/libtestoptimization.a *.go +GOARCH=arm64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" *.go echo -e "${GREEN}✓ ARM64 static library built${NC}" echo -e "${YELLOW}Building ARM64 dynamic library...${NC}" -GOARCH=arm64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib *.go -strip -x ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib +GOARCH=arm64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" *.go +strip -x "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" echo -e "${GREEN}✓ ARM64 dynamic library built${NC}" echo -e "${YELLOW}Building AMD64 static library...${NC}" -GOARCH=amd64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-x64-libtestoptimization-static/libtestoptimization.a *.go +GOARCH=amd64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" *.go echo -e "${GREEN}✓ AMD64 static library built${NC}" echo -e "${YELLOW}Building AMD64 dynamic library...${NC}" -GOARCH=amd64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib *.go -strip -x ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib +GOARCH=amd64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" *.go +strip -x "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" echo -e "${GREEN}✓ AMD64 dynamic library built${NC}" echo -e "${YELLOW}Creating universal binaries...${NC}" # Create universal static library -mkdir -p ./output/macos-libtestoptimization-static -lipo -create ./output/macos-arm64-libtestoptimization-static/libtestoptimization.a ./output/macos-x64-libtestoptimization-static/libtestoptimization.a -output ./output/macos-libtestoptimization-static/libtestoptimization.a -cp ./output/macos-arm64-libtestoptimization-static/libtestoptimization.h ./output/macos-libtestoptimization-static/libtestoptimization.h +mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-static" +lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.a" +cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.h" "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.h" # Create universal dynamic library -mkdir -p ./output/macos-libtestoptimization-dynamic -lipo -create ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib ./output/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib -output ./output/macos-libtestoptimization-dynamic/libtestoptimization.dylib -cp ./output/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h ./output/macos-libtestoptimization-dynamic/libtestoptimization.h +mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic" +lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.dylib" +cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h" "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.h" echo -e "${GREEN}✓ Universal binaries created${NC}" +# Clean up individual architecture folders since we have universal binaries +echo -e "${YELLOW}Cleaning up individual architecture folders...${NC}" +rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static" +rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic" +rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static" +rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic" +echo -e "${GREEN}✓ Individual architecture folders cleaned up${NC}" + echo -e "${YELLOW}Building for iOS...${NC}" -GOOS=ios GOARCH=arm64 CGO_ENABLED=1 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/ios-libtestoptimization-static/libtestoptimization.a *.go +GOOS=ios GOARCH=arm64 CGO_ENABLED=1 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/ios-libtestoptimization-static/libtestoptimization.a" *.go echo -e "${GREEN}✓ iOS library built${NC}" echo -e "${BLUE}--- Build Summary ---${NC}" echo -e "${GREEN}✓ All libraries built successfully!${NC}" -echo -e "${YELLOW}Output directory: $(pwd)/output${NC}" +echo -e "${YELLOW}Output directory: $ABS_OUTPUT_DIR${NC}" echo -e "${YELLOW}Available builds:${NC}" -echo " • macOS Universal Static: ./output/macos-libtestoptimization-static/" -echo " • macOS Universal Dynamic: ./output/macos-libtestoptimization-dynamic/" -echo " • macOS ARM64 Static: ./output/macos-arm64-libtestoptimization-static/" -echo " • macOS ARM64 Dynamic: ./output/macos-arm64-libtestoptimization-dynamic/" -echo " • macOS AMD64 Static: ./output/macos-x64-libtestoptimization-static/" -echo " • macOS AMD64 Dynamic: ./output/macos-x64-libtestoptimization-dynamic/" -echo " • iOS Static: ./output/ios-libtestoptimization-static/" +echo " • macOS Universal Static: $ABS_OUTPUT_DIR/macos-libtestoptimization-static/" +echo " • macOS Universal Dynamic: $ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/" +echo " • iOS Static: $ABS_OUTPUT_DIR/ios-libtestoptimization-static/" echo -e "${BLUE}=== Build completed successfully! ===${NC}" \ No newline at end of file From cd2c264a1a566e68be9c05520e5287841ea0d5bc Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:19:30 +0200 Subject: [PATCH 03/13] add localdev.sh script --- build/localdev.sh | 312 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100755 build/localdev.sh diff --git a/build/localdev.sh b/build/localdev.sh new file mode 100755 index 0000000..ec5fe81 --- /dev/null +++ b/build/localdev.sh @@ -0,0 +1,312 @@ +#!/bin/bash + +set -e # Exit on any error + +# Configuration +DEV_DIR="../dev" +OUTPUT_DIR="../dev-output" +REPO_URL="https://github.com/DataDog/dd-trace-go.git" +BRANCH="main" +TARGET_DIR="$DEV_DIR/internal/civisibility/native" + +# Colors for output +GREEN='\033[0;32m' +BLUE='\033[0;34m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Platform detection +PLATFORM=$(uname -s) +ARCH=$(uname -m) + +echo -e "${BLUE}=== Test Optimization Native Library - Local Development Build ===${NC}" +echo -e "${YELLOW}Detected platform: $PLATFORM ($ARCH)${NC}" + +# Check if Go is installed +if ! command -v go &> /dev/null; then + echo -e "${RED}Error: Go is not installed. Please install Go 1.24 or later.${NC}" + exit 1 +fi + +# Check Go version +GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//') +echo -e "${GREEN}✓ Found Go version: $GO_VERSION${NC}" + +# Platform-specific setup +case "$PLATFORM" in + "Darwin") + echo -e "${GREEN}✓ Running on macOS${NC}" + # Check if brew and coreutils are available + if ! command -v brew &> /dev/null; then + echo -e "${YELLOW}Warning: Homebrew not found. You may need to install coreutils manually.${NC}" + elif ! brew list coreutils &> /dev/null; then + echo -e "${YELLOW}Installing coreutils...${NC}" + brew install coreutils + fi + ;; + "Linux") + echo -e "${GREEN}✓ Running on Linux${NC}" + ;; + "MINGW"*|"MSYS"*|"CYGWIN"*) + echo -e "${GREEN}✓ Running on Windows${NC}" + ;; + *) + echo -e "${YELLOW}Warning: Unknown platform $PLATFORM. Proceeding anyway...${NC}" + ;; +esac + +# Function to check if directory is a git repository +is_git_repo() { + [ -d "$1/.git" ] +} + +# Function to get current branch +get_current_branch() { + git -C "$1" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "" +} + +echo -e "${BLUE}--- Setting up dd-trace-go repository ---${NC}" + +if [ -d "$DEV_DIR" ]; then + if is_git_repo "$DEV_DIR"; then + echo -e "${GREEN}✓ Found existing dd-trace-go repository in $DEV_DIR${NC}" + + # Check current branch + CURRENT_BRANCH=$(get_current_branch "$DEV_DIR") + if [ "$CURRENT_BRANCH" != "$BRANCH" ]; then + echo -e "${YELLOW}Current branch is '$CURRENT_BRANCH', switching to '$BRANCH'...${NC}" + git -C "$DEV_DIR" fetch origin + git -C "$DEV_DIR" checkout "$BRANCH" + fi + + # Pull latest changes + echo -e "${YELLOW}Pulling latest changes from $BRANCH...${NC}" + git -C "$DEV_DIR" pull origin "$BRANCH" + else + echo -e "${RED}Error: $DEV_DIR exists but is not a git repository${NC}" + echo -e "${YELLOW}Please remove $DEV_DIR directory and run this script again${NC}" + exit 1 + fi +else + echo -e "${YELLOW}Cloning dd-trace-go repository...${NC}" + git clone --branch "$BRANCH" "$REPO_URL" "$DEV_DIR" + echo -e "${GREEN}✓ Repository cloned successfully${NC}" +fi + +echo -e "${BLUE}--- Preparing build environment ---${NC}" + +# Create target directory +echo -e "${YELLOW}Creating target directory: $TARGET_DIR${NC}" +mkdir -p "$TARGET_DIR" + +# Copy build files +echo -e "${YELLOW}Copying build files...${NC}" +cp -rf ./* "$TARGET_DIR/" +echo -e "${GREEN}✓ Build files copied${NC}" + +# Copy native files +echo -e "${YELLOW}Copying native files...${NC}" +cp -rf ../native/* "$TARGET_DIR/" +echo -e "${GREEN}✓ Native files copied${NC}" + +# Create output directory at repo root +echo -e "${YELLOW}Creating output directory: $OUTPUT_DIR${NC}" +mkdir -p "$OUTPUT_DIR" + +# Get absolute path to output directory before changing directories +ABS_OUTPUT_DIR=$(realpath "$OUTPUT_DIR") + +# Change to target directory +cd "$TARGET_DIR" + +# Set common environment variables +export CGO_ENABLED=1 + +echo -e "${BLUE}--- Building Native Libraries ---${NC}" + +# Function to build static library +build_static() { + local goos=$1 + local goarch=$2 + local ext=$3 + local output_name=$4 + local cflags=$5 + local ldflags=$6 + + echo -e "${YELLOW}Building $goos-$goarch static library...${NC}" + GOOS="$goos" GOARCH="$goarch" CGO_CFLAGS="$cflags" CGO_LDFLAGS="$ldflags" \ + go build -tags civisibility_native -buildmode=c-archive \ + -ldflags="-s -w" -gcflags="all=-l" \ + -o "$ABS_OUTPUT_DIR/$output_name/libtestoptimization.$ext" *.go + echo -e "${GREEN}✓ $goos-$goarch static library built${NC}" +} + +# Function to build dynamic library +build_dynamic() { + local goos=$1 + local goarch=$2 + local ext=$3 + local output_name=$4 + local cflags=$5 + local ldflags=$6 + local strip_cmd=$7 + + echo -e "${YELLOW}Building $goos-$goarch dynamic library...${NC}" + GOOS="$goos" GOARCH="$goarch" CGO_CFLAGS="$cflags" CGO_LDFLAGS="$ldflags" \ + go build -tags civisibility_native -buildmode=c-shared \ + -ldflags="-s -w" -gcflags="all=-l" \ + -o "$ABS_OUTPUT_DIR/$output_name/libtestoptimization.$ext" *.go + + if [ -n "$strip_cmd" ] && command -v "$strip_cmd" &> /dev/null; then + $strip_cmd "$ABS_OUTPUT_DIR/$output_name/libtestoptimization.$ext" + fi + echo -e "${GREEN}✓ $goos-$goarch dynamic library built${NC}" +} + +# Platform-specific builds +case "$PLATFORM" in + "Darwin") + echo -e "${BLUE}--- Building for macOS and iOS ---${NC}" + + # macOS builds + MACOS_CFLAGS="-mmacosx-version-min=11.0 -O2 -Os -DNDEBUG -fdata-sections -ffunction-sections" + MACOS_LDFLAGS="-Wl,-x" + + build_static "darwin" "arm64" "a" "macos-arm64-libtestoptimization-static" "$MACOS_CFLAGS" "$MACOS_LDFLAGS" + build_dynamic "darwin" "arm64" "dylib" "macos-arm64-libtestoptimization-dynamic" "$MACOS_CFLAGS" "$MACOS_LDFLAGS" "strip -x" + + build_static "darwin" "amd64" "a" "macos-x64-libtestoptimization-static" "$MACOS_CFLAGS" "$MACOS_LDFLAGS" + build_dynamic "darwin" "amd64" "dylib" "macos-x64-libtestoptimization-dynamic" "$MACOS_CFLAGS" "$MACOS_LDFLAGS" "strip -x" + + # iOS build + echo -e "${YELLOW}Building iOS library...${NC}" + GOOS=ios GOARCH=arm64 CGO_ENABLED=1 \ + go build -tags civisibility_native -buildmode=c-archive \ + -ldflags="-s -w" -gcflags="all=-l" \ + -o "$ABS_OUTPUT_DIR/ios-libtestoptimization-static/libtestoptimization.a" *.go + echo -e "${GREEN}✓ iOS library built${NC}" + + # Create universal binaries + echo -e "${YELLOW}Creating universal binaries...${NC}" + + # Universal static library + mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-static" + lipo -create \ + "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" \ + "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" \ + -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.a" + cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.h" \ + "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.h" + + # Universal dynamic library + mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic" + lipo -create \ + "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" \ + "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" \ + -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.dylib" + cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h" \ + "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.h" + + echo -e "${GREEN}✓ Universal binaries created${NC}" + + # Clean up individual architecture folders + echo -e "${YELLOW}Cleaning up individual architecture folders...${NC}" + rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static" + rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic" + rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static" + rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic" + echo -e "${GREEN}✓ Individual architecture folders cleaned up${NC}" + ;; + + "Linux") + echo -e "${BLUE}--- Building for Linux and Android ---${NC}" + + # Linux builds + LINUX_CFLAGS="-O2 -Os -s -DNDEBUG -fdata-sections -ffunction-sections" + LINUX_LDFLAGS="-s -Wl,--gc-sections" + + build_static "linux" "arm64" "a" "linux-arm64-libtestoptimization-static" "$LINUX_CFLAGS" "$LINUX_LDFLAGS" + build_dynamic "linux" "arm64" "so" "linux-arm64-libtestoptimization-dynamic" "$LINUX_CFLAGS" "$LINUX_LDFLAGS" "strip --strip-unneeded" + + build_static "linux" "amd64" "a" "linux-x64-libtestoptimization-static" "$LINUX_CFLAGS" "$LINUX_LDFLAGS" + build_dynamic "linux" "amd64" "so" "linux-x64-libtestoptimization-dynamic" "$LINUX_CFLAGS" "$LINUX_LDFLAGS" "strip --strip-unneeded" + + # Android build (note: this is a simplified version, full Android NDK setup would be complex) + echo -e "${YELLOW}Building Android library (simplified - no NDK)...${NC}" + echo -e "${YELLOW}Note: For production Android builds, use the Docker workflow${NC}" + GOOS=android GOARCH=arm64 CGO_ENABLED=1 \ + go build -tags civisibility_native -buildmode=c-shared \ + -ldflags="-s -w" -gcflags="all=-l" \ + -o "$ABS_OUTPUT_DIR/android-arm64-libtestoptimization-dynamic/libtestoptimization.so" *.go 2>/dev/null || { + echo -e "${YELLOW}Warning: Android build failed (expected without NDK setup)${NC}" + echo -e "${YELLOW}Use the Docker workflow for proper Android builds${NC}" + } + ;; + + "MINGW"*|"MSYS"*|"CYGWIN"*) + echo -e "${BLUE}--- Building for Windows ---${NC}" + + # Windows builds + WINDOWS_CFLAGS="-O2 -fno-unwind-tables -fno-asynchronous-unwind-tables" + WINDOWS_LDFLAGS="-Wl,--no-seh" + + # Check for MinGW GCC + if command -v x86_64-w64-mingw32-gcc &> /dev/null; then + export CC=x86_64-w64-mingw32-gcc + elif command -v gcc &> /dev/null; then + export CC=gcc + else + echo -e "${RED}Error: No suitable C compiler found for Windows builds${NC}" + exit 1 + fi + + build_static "windows" "amd64" "lib" "windows-x64-libtestoptimization-static" "$WINDOWS_CFLAGS" "$WINDOWS_LDFLAGS" + build_dynamic "windows" "amd64" "dll" "windows-x64-libtestoptimization-dynamic" "$WINDOWS_CFLAGS" "$WINDOWS_LDFLAGS" "" + ;; + + *) + echo -e "${YELLOW}Building for current platform ($PLATFORM)...${NC}" + + # Generic builds for current platform + CURRENT_GOARCH="" + case "$ARCH" in + "x86_64"|"amd64") + CURRENT_GOARCH="amd64" + ;; + "arm64"|"aarch64") + CURRENT_GOARCH="arm64" + ;; + *) + echo -e "${RED}Error: Unsupported architecture $ARCH${NC}" + exit 1 + ;; + esac + + GENERIC_CFLAGS="-O2 -DNDEBUG" + GENERIC_LDFLAGS="" + + # Determine file extensions + if [[ "$PLATFORM" == *"MINGW"* ]] || [[ "$PLATFORM" == *"MSYS"* ]] || [[ "$PLATFORM" == *"CYGWIN"* ]]; then + build_static "$PLATFORM" "$CURRENT_GOARCH" "lib" "generic-libtestoptimization-static" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" + build_dynamic "$PLATFORM" "$CURRENT_GOARCH" "dll" "generic-libtestoptimization-dynamic" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" "" + else + build_static "$PLATFORM" "$CURRENT_GOARCH" "a" "generic-libtestoptimization-static" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" + build_dynamic "$PLATFORM" "$CURRENT_GOARCH" "so" "generic-libtestoptimization-dynamic" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" "strip" + fi + ;; +esac + +echo -e "${BLUE}--- Build Summary ---${NC}" +echo -e "${GREEN}✓ All libraries built successfully!${NC}" +echo -e "${YELLOW}Output directory: $ABS_OUTPUT_DIR${NC}" +echo -e "${YELLOW}Available builds:${NC}" + +# List output directories +for dir in "$ABS_OUTPUT_DIR"/*; do + if [ -d "$dir" ]; then + echo " • $(basename "$dir"): $dir/" + fi +done + +echo -e "${BLUE}=== Build completed successfully! ===${NC}" \ No newline at end of file From 0cac1070a9d04ab9ee4fa1809e09c23500a1473c Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:19:57 +0200 Subject: [PATCH 04/13] remove the osx-localdev.sh script --- build/osx-localdev.sh | 168 ------------------------------------------ 1 file changed, 168 deletions(-) delete mode 100755 build/osx-localdev.sh diff --git a/build/osx-localdev.sh b/build/osx-localdev.sh deleted file mode 100755 index 9c7e2cd..0000000 --- a/build/osx-localdev.sh +++ /dev/null @@ -1,168 +0,0 @@ -#!/bin/bash - -set -e # Exit on any error - -# Configuration -DEV_DIR="../dev" -OUTPUT_DIR="../dev-output" -REPO_URL="https://github.com/DataDog/dd-trace-go.git" -BRANCH="main" -TARGET_DIR="$DEV_DIR/internal/civisibility/native" - -# Colors for output -GREEN='\033[0;32m' -BLUE='\033[0;34m' -YELLOW='\033[1;33m' -RED='\033[0;31m' -NC='\033[0m' # No Color - -echo -e "${BLUE}=== Test Optimization Native Library - macOS Local Development Build ===${NC}" - -# Check if Go is installed -if ! command -v go &> /dev/null; then - echo -e "${RED}Error: Go is not installed. Please install Go 1.24 or later.${NC}" - exit 1 -fi - -# Check Go version -GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//') -echo -e "${GREEN}✓ Found Go version: $GO_VERSION${NC}" - -# Check if brew and coreutils are available -if ! command -v brew &> /dev/null; then - echo -e "${YELLOW}Warning: Homebrew not found. You may need to install coreutils manually.${NC}" -elif ! brew list coreutils &> /dev/null; then - echo -e "${YELLOW}Installing coreutils...${NC}" - brew install coreutils -fi - -# Function to check if directory is a git repository -is_git_repo() { - [ -d "$1/.git" ] -} - -# Function to get current branch -get_current_branch() { - git -C "$1" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "" -} - -# Function to check if repo is clean -is_repo_clean() { - [ -z "$(git -C "$1" status --porcelain)" ] -} - -echo -e "${BLUE}--- Setting up dd-trace-go repository ---${NC}" - -if [ -d "$DEV_DIR" ]; then - if is_git_repo "$DEV_DIR"; then - echo -e "${GREEN}✓ Found existing dd-trace-go repository in $DEV_DIR${NC}" - - # Check current branch - CURRENT_BRANCH=$(get_current_branch "$DEV_DIR") - if [ "$CURRENT_BRANCH" != "$BRANCH" ]; then - echo -e "${YELLOW}Current branch is '$CURRENT_BRANCH', switching to '$BRANCH'...${NC}" - git -C "$DEV_DIR" fetch origin - git -C "$DEV_DIR" checkout "$BRANCH" - fi - - # Pull latest changes - echo -e "${YELLOW}Pulling latest changes from $BRANCH...${NC}" - git -C "$DEV_DIR" pull origin "$BRANCH" - else - echo -e "${RED}Error: $DEV_DIR exists but is not a git repository${NC}" - echo -e "${YELLOW}Please remove $DEV_DIR directory and run this script again${NC}" - exit 1 - fi -else - echo -e "${YELLOW}Cloning dd-trace-go repository...${NC}" - git clone --branch "$BRANCH" "$REPO_URL" "$DEV_DIR" - echo -e "${GREEN}✓ Repository cloned successfully${NC}" -fi - -echo -e "${BLUE}--- Preparing build environment ---${NC}" - -# Create target directory -echo -e "${YELLOW}Creating target directory: $TARGET_DIR${NC}" -mkdir -p "$TARGET_DIR" - -# Copy build files -echo -e "${YELLOW}Copying build files...${NC}" -cp -rf ./* "$TARGET_DIR/" -echo -e "${GREEN}✓ Build files copied${NC}" - -# Copy native files -echo -e "${YELLOW}Copying native files...${NC}" -cp -rf ../native/* "$TARGET_DIR/" -echo -e "${GREEN}✓ Native files copied${NC}" - -echo -e "${BLUE}--- Building for macOS ---${NC}" - -# Create output directory at repo root -echo -e "${YELLOW}Creating output directory: $OUTPUT_DIR${NC}" -mkdir -p "$OUTPUT_DIR" - -# Get absolute path to output directory before changing directories -ABS_OUTPUT_DIR=$(realpath "$OUTPUT_DIR") - -# Change to target directory -cd "$TARGET_DIR" - -# Set build environment variables -export CGO_CFLAGS="-mmacosx-version-min=11.0 -O2 -Os -DNDEBUG -fdata-sections -ffunction-sections" -export CGO_LDFLAGS="-Wl,-x" -export GOOS=darwin -export CGO_ENABLED=1 - -echo -e "${YELLOW}Building ARM64 static library...${NC}" -GOARCH=arm64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" *.go -echo -e "${GREEN}✓ ARM64 static library built${NC}" - -echo -e "${YELLOW}Building ARM64 dynamic library...${NC}" -GOARCH=arm64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" *.go -strip -x "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" -echo -e "${GREEN}✓ ARM64 dynamic library built${NC}" - -echo -e "${YELLOW}Building AMD64 static library...${NC}" -GOARCH=amd64 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" *.go -echo -e "${GREEN}✓ AMD64 static library built${NC}" - -echo -e "${YELLOW}Building AMD64 dynamic library...${NC}" -GOARCH=amd64 go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" *.go -strip -x "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" -echo -e "${GREEN}✓ AMD64 dynamic library built${NC}" - -echo -e "${YELLOW}Creating universal binaries...${NC}" - -# Create universal static library -mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-static" -lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.a" -cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.h" "$ABS_OUTPUT_DIR/macos-libtestoptimization-static/libtestoptimization.h" - -# Create universal dynamic library -mkdir -p "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic" -lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" -output "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.dylib" -cp "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h" "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/libtestoptimization.h" - -echo -e "${GREEN}✓ Universal binaries created${NC}" - -# Clean up individual architecture folders since we have universal binaries -echo -e "${YELLOW}Cleaning up individual architecture folders...${NC}" -rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static" -rm -rf "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic" -rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static" -rm -rf "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic" -echo -e "${GREEN}✓ Individual architecture folders cleaned up${NC}" - -echo -e "${YELLOW}Building for iOS...${NC}" -GOOS=ios GOARCH=arm64 CGO_ENABLED=1 go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$ABS_OUTPUT_DIR/ios-libtestoptimization-static/libtestoptimization.a" *.go -echo -e "${GREEN}✓ iOS library built${NC}" - -echo -e "${BLUE}--- Build Summary ---${NC}" -echo -e "${GREEN}✓ All libraries built successfully!${NC}" -echo -e "${YELLOW}Output directory: $ABS_OUTPUT_DIR${NC}" -echo -e "${YELLOW}Available builds:${NC}" -echo " • macOS Universal Static: $ABS_OUTPUT_DIR/macos-libtestoptimization-static/" -echo " • macOS Universal Dynamic: $ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic/" -echo " • iOS Static: $ABS_OUTPUT_DIR/ios-libtestoptimization-static/" - -echo -e "${BLUE}=== Build completed successfully! ===${NC}" \ No newline at end of file From 4db3d8cb0e838d4af389eab5429f42eca6e3ae5c Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:36:44 +0200 Subject: [PATCH 05/13] improvements --- .gitignore | 3 +- build/localdev.sh | 212 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 213 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a3ee527..5a6089d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store .idea dev/ -dev-output/ \ No newline at end of file +dev-output/ +tools/ \ No newline at end of file diff --git a/build/localdev.sh b/build/localdev.sh index ec5fe81..77f4540 100755 --- a/build/localdev.sh +++ b/build/localdev.sh @@ -5,9 +5,11 @@ set -e # Exit on any error # Configuration DEV_DIR="../dev" OUTPUT_DIR="../dev-output" +TOOLS_DIR="../tools" REPO_URL="https://github.com/DataDog/dd-trace-go.git" BRANCH="main" TARGET_DIR="$DEV_DIR/internal/civisibility/native" +UPX_VERSION="5.0.0" # Colors for output GREEN='\033[0;32m' @@ -66,6 +68,99 @@ get_current_branch() { git -C "$1" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "" } +# Function to download and setup UPX +setup_upx() { + local upx_dir="$TOOLS_DIR/upx-$UPX_VERSION" + local upx_binary="" + + # Determine platform-specific UPX details + case "$PLATFORM" in + "Darwin") + local upx_archive="upx-${UPX_VERSION}-macos.tar.xz" + local upx_url="https://github.com/upx/upx/releases/download/v${UPX_VERSION}/${upx_archive}" + upx_binary="$upx_dir/upx" + ;; + "Linux") + local upx_archive="upx-${UPX_VERSION}-amd64_linux.tar.xz" + local upx_url="https://github.com/upx/upx/releases/download/v${UPX_VERSION}/${upx_archive}" + upx_binary="$upx_dir/upx" + ;; + "MINGW"*|"MSYS"*|"CYGWIN"*) + local upx_archive="upx-${UPX_VERSION}-win64.zip" + local upx_url="https://github.com/upx/upx/releases/download/v${UPX_VERSION}/${upx_archive}" + upx_binary="$upx_dir/upx-${UPX_VERSION}-win64/upx.exe" + ;; + *) + echo -e "${YELLOW}UPX not available for platform $PLATFORM${NC}" + return 1 + ;; + esac + + # Check if UPX is already available + if [ -f "$upx_binary" ]; then + echo -e "${GREEN}✓ Found cached UPX at $upx_binary${NC}" + echo "$upx_binary" + return 0 + fi + + echo -e "${YELLOW}Downloading UPX $UPX_VERSION for $PLATFORM...${NC}" + + # Create tools directory + mkdir -p "$TOOLS_DIR" + + # Download UPX + local temp_file="$TOOLS_DIR/$upx_archive" + if command -v wget &> /dev/null; then + wget -q "$upx_url" -O "$temp_file" + elif command -v curl &> /dev/null; then + curl -L -s "$upx_url" -o "$temp_file" + else + echo -e "${RED}Error: No download tool found (wget or curl)${NC}" + return 1 + fi + + if [ ! -f "$temp_file" ]; then + echo -e "${RED}Error: Failed to download UPX${NC}" + return 1 + fi + + # Extract UPX + echo -e "${YELLOW}Extracting UPX...${NC}" + case "$upx_archive" in + *.zip) + if command -v unzip &> /dev/null; then + unzip -q "$temp_file" -d "$upx_dir" + else + echo -e "${RED}Error: unzip not found${NC}" + return 1 + fi + ;; + *.tar.xz) + if command -v tar &> /dev/null; then + mkdir -p "$upx_dir" + tar -xf "$temp_file" -C "$upx_dir" --strip-components=1 + else + echo -e "${RED}Error: tar not found${NC}" + return 1 + fi + ;; + esac + + # Clean up download + rm -f "$temp_file" + + # Verify extraction + if [ -f "$upx_binary" ]; then + chmod +x "$upx_binary" 2>/dev/null || true + echo -e "${GREEN}✓ UPX $UPX_VERSION installed to $upx_binary${NC}" + echo "$upx_binary" + return 0 + else + echo -e "${RED}Error: UPX extraction failed${NC}" + return 1 + fi +} + echo -e "${BLUE}--- Setting up dd-trace-go repository ---${NC}" if [ -d "$DEV_DIR" ]; then @@ -114,6 +209,10 @@ echo -e "${GREEN}✓ Native files copied${NC}" echo -e "${YELLOW}Creating output directory: $OUTPUT_DIR${NC}" mkdir -p "$OUTPUT_DIR" +# Create tools directory for cached tools +echo -e "${YELLOW}Creating tools directory: $TOOLS_DIR${NC}" +mkdir -p "$TOOLS_DIR" + # Get absolute path to output directory before changing directories ABS_OUTPUT_DIR=$(realpath "$OUTPUT_DIR") @@ -263,6 +362,102 @@ case "$PLATFORM" in build_static "windows" "amd64" "lib" "windows-x64-libtestoptimization-static" "$WINDOWS_CFLAGS" "$WINDOWS_LDFLAGS" build_dynamic "windows" "amd64" "dll" "windows-x64-libtestoptimization-dynamic" "$WINDOWS_CFLAGS" "$WINDOWS_LDFLAGS" "" + + # Windows-specific post-processing + echo -e "${YELLOW}Post-processing Windows libraries...${NC}" + + # Strip .pdata/.xdata sections from .lib file (fixes MSVC 14.44 linker issues) + LIB_DIR="$ABS_OUTPUT_DIR/windows-x64-libtestoptimization-static" + LIB_FILE="$LIB_DIR/libtestoptimization.lib" + + if [ -f "$LIB_FILE" ]; then + echo -e "${YELLOW}Removing .pdata/.xdata sections from static library...${NC}" + + # Try to find llvm-ar and llvm-objcopy + LLVM_AR="" + LLVM_OBJCOPY="" + + # Check common locations for LLVM tools + for path in "/usr/bin" "/mingw64/bin" "/c/mingw64/bin" "$MINGW_PATH/mingw64/bin"; do + if [ -f "$path/llvm-ar.exe" ] || [ -f "$path/llvm-ar" ]; then + LLVM_AR="$path/llvm-ar" + [ -f "$path/llvm-ar.exe" ] && LLVM_AR="$path/llvm-ar.exe" + fi + if [ -f "$path/llvm-objcopy.exe" ] || [ -f "$path/llvm-objcopy" ]; then + LLVM_OBJCOPY="$path/llvm-objcopy" + [ -f "$path/llvm-objcopy.exe" ] && LLVM_OBJCOPY="$path/llvm-objcopy.exe" + fi + if [ -n "$LLVM_AR" ] && [ -n "$LLVM_OBJCOPY" ]; then + break + fi + done + + # Fallback to system ar/objcopy if llvm tools not found + if [ -z "$LLVM_AR" ]; then + if command -v ar &> /dev/null; then + LLVM_AR="ar" + else + echo -e "${YELLOW}Warning: ar tool not found, skipping .lib post-processing${NC}" + fi + fi + + if [ -z "$LLVM_OBJCOPY" ]; then + if command -v objcopy &> /dev/null; then + LLVM_OBJCOPY="objcopy" + else + echo -e "${YELLOW}Warning: objcopy tool not found, skipping .lib post-processing${NC}" + fi + fi + + if [ -n "$LLVM_AR" ] && [ -n "$LLVM_OBJCOPY" ]; then + echo -e "${YELLOW}Using $LLVM_AR and $LLVM_OBJCOPY for post-processing${NC}" + + # Create temporary directory for processing + TEMP_DIR=$(mktemp -d) + cp "$LIB_FILE" "$TEMP_DIR/" + + pushd "$TEMP_DIR" > /dev/null + + # Extract all object files + "$LLVM_AR" x libtestoptimization.lib + + # Strip problematic sections from each object file + for obj_file in *.o; do + if [ -f "$obj_file" ]; then + echo -e "${YELLOW} • cleaning $obj_file${NC}" + "$LLVM_OBJCOPY" --remove-section=.pdata --remove-section=.xdata "$obj_file" 2>/dev/null || true + fi + done + + # Rebuild the static library + rm -f libtestoptimization.lib + "$LLVM_AR" rc libtestoptimization.lib *.o + "$LLVM_AR" s libtestoptimization.lib # rebuild symbol index + + # Copy back the processed library + cp libtestoptimization.lib "$LIB_FILE" + + popd > /dev/null + rm -rf "$TEMP_DIR" + + echo -e "${GREEN}✓ Static library post-processed${NC}" + fi + fi + + # Compress DLL with UPX + DLL_FILE="$ABS_OUTPUT_DIR/windows-x64-libtestoptimization-dynamic/libtestoptimization.dll" + if [ -f "$DLL_FILE" ]; then + UPX_BINARY=$(setup_upx) + if [ $? -eq 0 ] && [ -n "$UPX_BINARY" ]; then + echo -e "${YELLOW}Compressing DLL with UPX...${NC}" + "$UPX_BINARY" --best --lzma "$DLL_FILE" 2>/dev/null || { + echo -e "${YELLOW}Warning: UPX compression failed, continuing...${NC}" + } + echo -e "${GREEN}✓ DLL compressed${NC}" + else + echo -e "${YELLOW}Note: UPX setup failed, skipping DLL compression${NC}" + fi + fi ;; *) @@ -286,10 +481,25 @@ case "$PLATFORM" in GENERIC_CFLAGS="-O2 -DNDEBUG" GENERIC_LDFLAGS="" - # Determine file extensions + # Determine file extensions and build if [[ "$PLATFORM" == *"MINGW"* ]] || [[ "$PLATFORM" == *"MSYS"* ]] || [[ "$PLATFORM" == *"CYGWIN"* ]]; then build_static "$PLATFORM" "$CURRENT_GOARCH" "lib" "generic-libtestoptimization-static" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" build_dynamic "$PLATFORM" "$CURRENT_GOARCH" "dll" "generic-libtestoptimization-dynamic" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" "" + + # Compress DLL with UPX + DLL_FILE="$ABS_OUTPUT_DIR/generic-libtestoptimization-dynamic/libtestoptimization.dll" + if [ -f "$DLL_FILE" ]; then + UPX_BINARY=$(setup_upx) + if [ $? -eq 0 ] && [ -n "$UPX_BINARY" ]; then + echo -e "${YELLOW}Compressing DLL with UPX...${NC}" + "$UPX_BINARY" --best --lzma "$DLL_FILE" 2>/dev/null || { + echo -e "${YELLOW}Warning: UPX compression failed, continuing...${NC}" + } + echo -e "${GREEN}✓ DLL compressed${NC}" + else + echo -e "${YELLOW}Note: UPX setup failed, skipping DLL compression${NC}" + fi + fi else build_static "$PLATFORM" "$CURRENT_GOARCH" "a" "generic-libtestoptimization-static" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" build_dynamic "$PLATFORM" "$CURRENT_GOARCH" "so" "generic-libtestoptimization-dynamic" "$GENERIC_CFLAGS" "$GENERIC_LDFLAGS" "strip" From 42eb68e676d2ed600f1b9db0d3fec35986de2623 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:41:43 +0200 Subject: [PATCH 06/13] powershell version --- build/localdev.cmd | 81 ++++++ build/localdev.ps1 | 662 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 743 insertions(+) create mode 100644 build/localdev.cmd create mode 100644 build/localdev.ps1 diff --git a/build/localdev.cmd b/build/localdev.cmd new file mode 100644 index 0000000..9209642 --- /dev/null +++ b/build/localdev.cmd @@ -0,0 +1,81 @@ +@echo off +setlocal enabledelayedexpansion + +REM Test Optimization Native Library - Local Development Build Script (CMD Wrapper) +REM This script calls the PowerShell version of localdev.ps1 + +echo Test Optimization Native Library - Local Development Build +echo. + +REM Check if PowerShell is available +powershell -Command "exit 0" >nul 2>&1 +if !errorlevel! neq 0 ( + echo Error: PowerShell is not available or not in PATH + echo Please ensure PowerShell is installed and accessible + exit /b 1 +) + +REM Get the directory where this script is located +set "SCRIPT_DIR=%~dp0" + +REM Check if localdev.ps1 exists +if not exist "%SCRIPT_DIR%localdev.ps1" ( + echo Error: localdev.ps1 not found in %SCRIPT_DIR% + echo Please ensure localdev.ps1 is in the same directory as this script + exit /b 1 +) + +REM Show help if requested +if /i "%1"=="--help" goto :show_help +if /i "%1"=="-h" goto :show_help +if /i "%1"=="/?" goto :show_help + +echo Executing PowerShell build script... +echo. + +REM Execute the PowerShell script with bypass execution policy +REM This allows the script to run without requiring signed scripts +powershell -ExecutionPolicy Bypass -File "%SCRIPT_DIR%localdev.ps1" %* + +REM Capture the exit code from PowerShell +set "PS_EXIT_CODE=!errorlevel!" + +REM Check if PowerShell script executed successfully +if !PS_EXIT_CODE! equ 0 ( + echo. + echo Build script completed successfully! +) else ( + echo. + echo Build script failed with exit code !PS_EXIT_CODE! + echo Please check the output above for error details +) + +REM Exit with the same code as PowerShell +exit /b !PS_EXIT_CODE! + +:show_help +echo. +echo Test Optimization Native Library - Local Development Build Script +echo. +echo This script builds the native library for local development by: +echo 1. Setting up the dd-trace-go repository +echo 2. Copying build and native files +echo 3. Building platform-specific libraries +echo 4. Applying platform-specific optimizations +echo. +echo Usage: +echo localdev.cmd [options] +echo. +echo Options: +echo --help, -h, /? Show this help message +echo. +echo Requirements: +echo - Go 1.24 or later +echo - Git +echo - PowerShell +echo - Platform-specific build tools (MinGW for Windows, etc.) +echo. +echo Output: +echo All build artifacts will be placed in ../dev-output/ +echo. +exit /b 0 \ No newline at end of file diff --git a/build/localdev.ps1 b/build/localdev.ps1 new file mode 100644 index 0000000..0eeb245 --- /dev/null +++ b/build/localdev.ps1 @@ -0,0 +1,662 @@ +# PowerShell Local Development Build Script for Test Optimization Native Library +param( + [switch]$Help +) + +if ($Help) { + Write-Host @" +Test Optimization Native Library - Local Development Build Script + +This script builds the native library for local development by: +1. Setting up the dd-trace-go repository +2. Copying build and native files +3. Building platform-specific libraries +4. Applying platform-specific optimizations + +Usage: + .\localdev.ps1 + +Requirements: + - Go 1.24 or later + - Git + - Platform-specific build tools (MinGW for Windows, etc.) + +Output: + All build artifacts will be placed in ../dev-output/ +"@ + exit 0 +} + +# Enable strict mode and stop on errors +$ErrorActionPreference = "Stop" +Set-StrictMode -Version Latest + +# Configuration +$DEV_DIR = "../dev" +$OUTPUT_DIR = "../dev-output" +$TOOLS_DIR = "../tools" +$REPO_URL = "https://github.com/DataDog/dd-trace-go.git" +$BRANCH = "main" +$TARGET_DIR = "$DEV_DIR/internal/civisibility/native" +$UPX_VERSION = "5.0.0" + +# Colors for output (using Write-Host with colors) +function Write-ColorOutput { + param( + [string]$Message, + [string]$Color = "White" + ) + Write-Host $Message -ForegroundColor $Color +} + +function Write-Blue { param([string]$Message) Write-ColorOutput $Message "Blue" } +function Write-Green { param([string]$Message) Write-ColorOutput $Message "Green" } +function Write-Yellow { param([string]$Message) Write-ColorOutput $Message "Yellow" } +function Write-Red { param([string]$Message) Write-ColorOutput $Message "Red" } + +# Platform detection +$PLATFORM = [System.Environment]::OSVersion.Platform +$ARCH = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") +if (-not $ARCH) { + $ARCH = $env:PROCESSOR_ARCHITECTURE + if (-not $ARCH) { + $ARCH = "AMD64" # Default assumption + } +} + +# Normalize platform names +$PlatformName = switch ($PLATFORM) { + "Win32NT" { "Windows" } + "Unix" { + if ($IsLinux) { "Linux" } + elseif ($IsMacOS) { "Darwin" } + else { "Unix" } + } + default { "Windows" } # Default to Windows for PowerShell +} + +Write-Blue "=== Test Optimization Native Library - Local Development Build ===" +Write-Yellow "Detected platform: $PlatformName ($ARCH)" + +# Check if Go is installed +try { + $goVersion = (go version 2>$null) + if (-not $goVersion) { + throw "Go not found" + } + $goVersionNumber = ($goVersion -split '\s+')[2] -replace '^go', '' + Write-Green "✓ Found Go version: $goVersionNumber" +} catch { + Write-Red "Error: Go is not installed. Please install Go 1.24 or later." + exit 1 +} + +# Platform-specific setup +switch ($PlatformName) { + "Windows" { + Write-Green "✓ Running on Windows" + } + "Darwin" { + Write-Green "✓ Running on macOS" + # Check if brew and coreutils are available + try { + $null = Get-Command brew -ErrorAction Stop + try { + & brew list coreutils 2>$null | Out-Null + } catch { + Write-Yellow "Installing coreutils..." + & brew install coreutils + } + } catch { + Write-Yellow "Warning: Homebrew not found. You may need to install coreutils manually." + } + } + "Linux" { + Write-Green "✓ Running on Linux" + } + default { + Write-Yellow "Warning: Unknown platform $PlatformName. Proceeding anyway..." + } +} + +# Function to check if directory is a git repository +function Test-GitRepository { + param([string]$Path) + return Test-Path (Join-Path $Path ".git") +} + +# Function to get current branch +function Get-CurrentBranch { + param([string]$Path) + try { + $currentDir = Get-Location + Set-Location $Path + $branch = & git rev-parse --abbrev-ref HEAD 2>$null + Set-Location $currentDir + return $branch + } catch { + Set-Location $currentDir + return "" + } +} + +# Function to download and setup UPX +function Set-UpUPX { + $upxDir = "$TOOLS_DIR/upx-$UPX_VERSION" + $upxBinary = "" + + # Determine platform-specific UPX details + switch ($PlatformName) { + "Darwin" { + $upxArchive = "upx-$UPX_VERSION-macos.tar.xz" + $upxUrl = "https://github.com/upx/upx/releases/download/v$UPX_VERSION/$upxArchive" + $upxBinary = "$upxDir/upx" + } + "Linux" { + $upxArchive = "upx-$UPX_VERSION-amd64_linux.tar.xz" + $upxUrl = "https://github.com/upx/upx/releases/download/v$UPX_VERSION/$upxArchive" + $upxBinary = "$upxDir/upx" + } + "Windows" { + $upxArchive = "upx-$UPX_VERSION-win64.zip" + $upxUrl = "https://github.com/upx/upx/releases/download/v$UPX_VERSION/$upxArchive" + $upxBinary = "$upxDir/upx-$UPX_VERSION-win64/upx.exe" + } + default { + Write-Yellow "UPX not available for platform $PlatformName" + return $null + } + } + + # Check if UPX is already available + if (Test-Path $upxBinary) { + Write-Green "✓ Found cached UPX at $upxBinary" + return $upxBinary + } + + Write-Yellow "Downloading UPX $UPX_VERSION for $PlatformName..." + + # Create tools directory + if (-not (Test-Path $TOOLS_DIR)) { + New-Item -ItemType Directory -Path $TOOLS_DIR -Force | Out-Null + } + + # Download UPX + $tempFile = "$TOOLS_DIR/$upxArchive" + try { + Invoke-WebRequest -Uri $upxUrl -OutFile $tempFile -UseBasicParsing + } catch { + Write-Red "Error: Failed to download UPX" + return $null + } + + if (-not (Test-Path $tempFile)) { + Write-Red "Error: Failed to download UPX" + return $null + } + + # Extract UPX + Write-Yellow "Extracting UPX..." + try { + if ($upxArchive -like "*.zip") { + Expand-Archive -Path $tempFile -DestinationPath $upxDir -Force + } elseif ($upxArchive -like "*.tar.xz") { + # For tar.xz files, we need to use external tools or handle differently + if (-not (Test-Path $upxDir)) { + New-Item -ItemType Directory -Path $upxDir -Force | Out-Null + } + # Try using tar if available + try { + & tar -xf $tempFile -C $upxDir --strip-components=1 + } catch { + Write-Red "Error: tar not found or failed to extract" + return $null + } + } + } catch { + Write-Red "Error: UPX extraction failed" + return $null + } + + # Clean up download + Remove-Item $tempFile -Force + + # Verify extraction + if (Test-Path $upxBinary) { + Write-Green "✓ UPX $UPX_VERSION installed to $upxBinary" + return $upxBinary + } else { + Write-Red "Error: UPX extraction failed" + return $null + } +} + +Write-Blue "--- Setting up dd-trace-go repository ---" + +if (Test-Path $DEV_DIR) { + if (Test-GitRepository $DEV_DIR) { + Write-Green "✓ Found existing dd-trace-go repository in $DEV_DIR" + + # Check current branch + $currentBranch = Get-CurrentBranch $DEV_DIR + if ($currentBranch -ne $BRANCH) { + Write-Yellow "Current branch is '$currentBranch', switching to '$BRANCH'..." + $currentDir = Get-Location + Set-Location $DEV_DIR + & git fetch origin + & git checkout $BRANCH + Set-Location $currentDir + } + + # Pull latest changes + Write-Yellow "Pulling latest changes from $BRANCH..." + $currentDir = Get-Location + Set-Location $DEV_DIR + & git pull origin $BRANCH + Set-Location $currentDir + } else { + Write-Red "Error: $DEV_DIR exists but is not a git repository" + Write-Yellow "Please remove $DEV_DIR directory and run this script again" + exit 1 + } +} else { + Write-Yellow "Cloning dd-trace-go repository..." + & git clone --branch $BRANCH $REPO_URL $DEV_DIR + Write-Green "✓ Repository cloned successfully" +} + +Write-Blue "--- Preparing build environment ---" + +# Create target directory +Write-Yellow "Creating target directory: $TARGET_DIR" +if (-not (Test-Path $TARGET_DIR)) { + New-Item -ItemType Directory -Path $TARGET_DIR -Force | Out-Null +} + +# Copy build files +Write-Yellow "Copying build files..." +Copy-Item -Path "./*" -Destination $TARGET_DIR -Recurse -Force +Write-Green "✓ Build files copied" + +# Copy native files +Write-Yellow "Copying native files..." +Copy-Item -Path "../native/*" -Destination $TARGET_DIR -Recurse -Force +Write-Green "✓ Native files copied" + +# Create output directory at repo root +Write-Yellow "Creating output directory: $OUTPUT_DIR" +if (-not (Test-Path $OUTPUT_DIR)) { + New-Item -ItemType Directory -Path $OUTPUT_DIR -Force | Out-Null +} + +# Create tools directory for cached tools +Write-Yellow "Creating tools directory: $TOOLS_DIR" +if (-not (Test-Path $TOOLS_DIR)) { + New-Item -ItemType Directory -Path $TOOLS_DIR -Force | Out-Null +} + +# Get absolute path to output directory before changing directories +$ABS_OUTPUT_DIR = (Resolve-Path $OUTPUT_DIR).Path + +# Change to target directory +$originalLocation = Get-Location +Set-Location $TARGET_DIR + +# Set common environment variables +$env:CGO_ENABLED = "1" + +Write-Blue "--- Building Native Libraries ---" + +# Function to build static library +function Build-StaticLibrary { + param( + [string]$GOOS, + [string]$GOARCH, + [string]$Extension, + [string]$OutputName, + [string]$CFlags, + [string]$LDFlags + ) + + Write-Yellow "Building $GOOS-$GOARCH static library..." + + $env:GOOS = $GOOS + $env:GOARCH = $GOARCH + $env:CGO_CFLAGS = $CFlags + $env:CGO_LDFLAGS = $LDFlags + + $outputPath = "$ABS_OUTPUT_DIR/$OutputName" + if (-not (Test-Path $outputPath)) { + New-Item -ItemType Directory -Path $outputPath -Force | Out-Null + } + + & go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$outputPath/libtestoptimization.$Extension" (Get-ChildItem "*.go") + + Write-Green "✓ $GOOS-$GOARCH static library built" +} + +# Function to build dynamic library +function Build-DynamicLibrary { + param( + [string]$GOOS, + [string]$GOARCH, + [string]$Extension, + [string]$OutputName, + [string]$CFlags, + [string]$LDFlags, + [string]$StripCommand = "" + ) + + Write-Yellow "Building $GOOS-$GOARCH dynamic library..." + + $env:GOOS = $GOOS + $env:GOARCH = $GOARCH + $env:CGO_CFLAGS = $CFlags + $env:CGO_LDFLAGS = $LDFlags + + $outputPath = "$ABS_OUTPUT_DIR/$OutputName" + if (-not (Test-Path $outputPath)) { + New-Item -ItemType Directory -Path $outputPath -Force | Out-Null + } + + & go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$outputPath/libtestoptimization.$Extension" (Get-ChildItem "*.go") + + if ($StripCommand -and (Get-Command ($StripCommand -split '\s+')[0] -ErrorAction SilentlyContinue)) { + $stripArgs = ($StripCommand -split '\s+')[1..($StripCommand -split '\s+').Length] + & ($StripCommand -split '\s+')[0] @stripArgs "$outputPath/libtestoptimization.$Extension" + } + + Write-Green "✓ $GOOS-$GOARCH dynamic library built" +} + +# Platform-specific builds +switch ($PlatformName) { + "Darwin" { + Write-Blue "--- Building for macOS and iOS ---" + + # macOS builds + $MACOS_CFLAGS = "-mmacosx-version-min=11.0 -O2 -Os -DNDEBUG -fdata-sections -ffunction-sections" + $MACOS_LDFLAGS = "-Wl,-x" + + Build-StaticLibrary "darwin" "arm64" "a" "macos-arm64-libtestoptimization-static" $MACOS_CFLAGS $MACOS_LDFLAGS + Build-DynamicLibrary "darwin" "arm64" "dylib" "macos-arm64-libtestoptimization-dynamic" $MACOS_CFLAGS $MACOS_LDFLAGS "strip -x" + + Build-StaticLibrary "darwin" "amd64" "a" "macos-x64-libtestoptimization-static" $MACOS_CFLAGS $MACOS_LDFLAGS + Build-DynamicLibrary "darwin" "amd64" "dylib" "macos-x64-libtestoptimization-dynamic" $MACOS_CFLAGS $MACOS_LDFLAGS "strip -x" + + # iOS build + Write-Yellow "Building iOS library..." + $env:GOOS = "ios" + $env:GOARCH = "arm64" + $env:CGO_ENABLED = "1" + + $iosPath = "$ABS_OUTPUT_DIR/ios-libtestoptimization-static" + if (-not (Test-Path $iosPath)) { + New-Item -ItemType Directory -Path $iosPath -Force | Out-Null + } + + & go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o "$iosPath/libtestoptimization.a" (Get-ChildItem "*.go") + Write-Green "✓ iOS library built" + + # Create universal binaries + Write-Yellow "Creating universal binaries..." + + # Universal static library + $universalStaticPath = "$ABS_OUTPUT_DIR/macos-libtestoptimization-static" + if (-not (Test-Path $universalStaticPath)) { + New-Item -ItemType Directory -Path $universalStaticPath -Force | Out-Null + } + + & lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.a" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static/libtestoptimization.a" -output "$universalStaticPath/libtestoptimization.a" + Copy-Item "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static/libtestoptimization.h" "$universalStaticPath/libtestoptimization.h" + + # Universal dynamic library + $universalDynamicPath = "$ABS_OUTPUT_DIR/macos-libtestoptimization-dynamic" + if (-not (Test-Path $universalDynamicPath)) { + New-Item -ItemType Directory -Path $universalDynamicPath -Force | Out-Null + } + + & lipo -create "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.dylib" "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic/libtestoptimization.dylib" -output "$universalDynamicPath/libtestoptimization.dylib" + Copy-Item "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic/libtestoptimization.h" "$universalDynamicPath/libtestoptimization.h" + + Write-Green "✓ Universal binaries created" + + # Clean up individual architecture folders + Write-Yellow "Cleaning up individual architecture folders..." + Remove-Item "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-static" -Recurse -Force + Remove-Item "$ABS_OUTPUT_DIR/macos-arm64-libtestoptimization-dynamic" -Recurse -Force + Remove-Item "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-static" -Recurse -Force + Remove-Item "$ABS_OUTPUT_DIR/macos-x64-libtestoptimization-dynamic" -Recurse -Force + Write-Green "✓ Individual architecture folders cleaned up" + } + + "Linux" { + Write-Blue "--- Building for Linux and Android ---" + + # Linux builds + $LINUX_CFLAGS = "-O2 -Os -s -DNDEBUG -fdata-sections -ffunction-sections" + $LINUX_LDFLAGS = "-s -Wl,--gc-sections" + + Build-StaticLibrary "linux" "arm64" "a" "linux-arm64-libtestoptimization-static" $LINUX_CFLAGS $LINUX_LDFLAGS + Build-DynamicLibrary "linux" "arm64" "so" "linux-arm64-libtestoptimization-dynamic" $LINUX_CFLAGS $LINUX_LDFLAGS "strip --strip-unneeded" + + Build-StaticLibrary "linux" "amd64" "a" "linux-x64-libtestoptimization-static" $LINUX_CFLAGS $LINUX_LDFLAGS + Build-DynamicLibrary "linux" "amd64" "so" "linux-x64-libtestoptimization-dynamic" $LINUX_CFLAGS $LINUX_LDFLAGS "strip --strip-unneeded" + + # Android build (simplified version, full Android NDK setup would be complex) + Write-Yellow "Building Android library (simplified - no NDK)..." + Write-Yellow "Note: For production Android builds, use the Docker workflow" + + try { + $env:GOOS = "android" + $env:GOARCH = "arm64" + $env:CGO_ENABLED = "1" + + $androidPath = "$ABS_OUTPUT_DIR/android-arm64-libtestoptimization-dynamic" + if (-not (Test-Path $androidPath)) { + New-Item -ItemType Directory -Path $androidPath -Force | Out-Null + } + + & go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o "$androidPath/libtestoptimization.so" (Get-ChildItem "*.go") 2>$null + } catch { + Write-Yellow "Warning: Android build failed (expected without NDK setup)" + Write-Yellow "Use the Docker workflow for proper Android builds" + } + } + + "Windows" { + Write-Blue "--- Building for Windows ---" + + # Windows builds + $WINDOWS_CFLAGS = "-O2 -fno-unwind-tables -fno-asynchronous-unwind-tables" + $WINDOWS_LDFLAGS = "-Wl,--no-seh" + + # Check for MinGW GCC + $CC = $null + try { + $null = Get-Command "x86_64-w64-mingw32-gcc" -ErrorAction Stop + $CC = "x86_64-w64-mingw32-gcc" + } catch { + try { + $null = Get-Command "gcc" -ErrorAction Stop + $CC = "gcc" + } catch { + Write-Red "Error: No suitable C compiler found for Windows builds" + exit 1 + } + } + $env:CC = $CC + + Build-StaticLibrary "windows" "amd64" "lib" "windows-x64-libtestoptimization-static" $WINDOWS_CFLAGS $WINDOWS_LDFLAGS + Build-DynamicLibrary "windows" "amd64" "dll" "windows-x64-libtestoptimization-dynamic" $WINDOWS_CFLAGS $WINDOWS_LDFLAGS + + # Windows-specific post-processing + Write-Yellow "Post-processing Windows libraries..." + + # Strip .pdata/.xdata sections from .lib file (fixes MSVC 14.44 linker issues) + $libDir = "$ABS_OUTPUT_DIR/windows-x64-libtestoptimization-static" + $libFile = "$libDir/libtestoptimization.lib" + + if (Test-Path $libFile) { + Write-Yellow "Removing .pdata/.xdata sections from static library..." + + # Try to find llvm-ar and llvm-objcopy + $LLVM_AR = $null + $LLVM_OBJCOPY = $null + + # Check common locations for LLVM tools + $searchPaths = @("/usr/bin", "/mingw64/bin", "/c/mingw64/bin", "$env:MINGW_PATH/mingw64/bin") + + foreach ($path in $searchPaths) { + if (Test-Path "$path/llvm-ar.exe") { + $LLVM_AR = "$path/llvm-ar.exe" + } elseif (Test-Path "$path/llvm-ar") { + $LLVM_AR = "$path/llvm-ar" + } + + if (Test-Path "$path/llvm-objcopy.exe") { + $LLVM_OBJCOPY = "$path/llvm-objcopy.exe" + } elseif (Test-Path "$path/llvm-objcopy") { + $LLVM_OBJCOPY = "$path/llvm-objcopy" + } + + if ($LLVM_AR -and $LLVM_OBJCOPY) { + break + } + } + + # Fallback to system ar/objcopy if llvm tools not found + if (-not $LLVM_AR) { + try { + $null = Get-Command "ar" -ErrorAction Stop + $LLVM_AR = "ar" + } catch { + Write-Yellow "Warning: ar tool not found, skipping .lib post-processing" + } + } + + if (-not $LLVM_OBJCOPY) { + try { + $null = Get-Command "objcopy" -ErrorAction Stop + $LLVM_OBJCOPY = "objcopy" + } catch { + Write-Yellow "Warning: objcopy tool not found, skipping .lib post-processing" + } + } + + if ($LLVM_AR -and $LLVM_OBJCOPY) { + Write-Yellow "Using $LLVM_AR and $LLVM_OBJCOPY for post-processing" + + # Create temporary directory for processing + $tempDir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_ } + Copy-Item $libFile $tempDir + + Push-Location $tempDir + + try { + # Extract all object files + & $LLVM_AR x "libtestoptimization.lib" + + # Strip problematic sections from each object file + Get-ChildItem "*.o" | ForEach-Object { + Write-Yellow " • cleaning $($_.Name)" + & $LLVM_OBJCOPY --remove-section=.pdata --remove-section=.xdata $_.FullName 2>$null + } + + # Rebuild the static library + Remove-Item "libtestoptimization.lib" -Force + & $LLVM_AR rc "libtestoptimization.lib" (Get-ChildItem "*.o") + & $LLVM_AR s "libtestoptimization.lib" # rebuild symbol index + + # Copy back the processed library + Copy-Item "libtestoptimization.lib" $libFile -Force + } finally { + Pop-Location + Remove-Item $tempDir -Recurse -Force + } + + Write-Green "✓ Static library post-processed" + } + } + + # Compress DLL with UPX + $dllFile = "$ABS_OUTPUT_DIR/windows-x64-libtestoptimization-dynamic/libtestoptimization.dll" + if (Test-Path $dllFile) { + $upxBinary = Set-UpUPX + if ($upxBinary) { + Write-Yellow "Compressing DLL with UPX..." + try { + & $upxBinary --best --lzma $dllFile 2>$null + Write-Green "✓ DLL compressed" + } catch { + Write-Yellow "Warning: UPX compression failed, continuing..." + } + } else { + Write-Yellow "Note: UPX setup failed, skipping DLL compression" + } + } + } + + default { + Write-Yellow "Building for current platform ($PlatformName)..." + + # Generic builds for current platform + $CURRENT_GOARCH = "" + switch ($ARCH) { + { $_ -in @("x86_64", "amd64", "AMD64") } { + $CURRENT_GOARCH = "amd64" + } + { $_ -in @("arm64", "aarch64", "ARM64") } { + $CURRENT_GOARCH = "arm64" + } + default { + Write-Red "Error: Unsupported architecture $ARCH" + exit 1 + } + } + + $GENERIC_CFLAGS = "-O2 -DNDEBUG" + $GENERIC_LDFLAGS = "" + + # Determine file extensions and build + if ($PlatformName -like "*Windows*") { + Build-StaticLibrary $PlatformName $CURRENT_GOARCH "lib" "generic-libtestoptimization-static" $GENERIC_CFLAGS $GENERIC_LDFLAGS + Build-DynamicLibrary $PlatformName $CURRENT_GOARCH "dll" "generic-libtestoptimization-dynamic" $GENERIC_CFLAGS $GENERIC_LDFLAGS + + # Compress DLL with UPX + $dllFile = "$ABS_OUTPUT_DIR/generic-libtestoptimization-dynamic/libtestoptimization.dll" + if (Test-Path $dllFile) { + $upxBinary = Set-UpUPX + if ($upxBinary) { + Write-Yellow "Compressing DLL with UPX..." + try { + & $upxBinary --best --lzma $dllFile 2>$null + Write-Green "✓ DLL compressed" + } catch { + Write-Yellow "Warning: UPX compression failed, continuing..." + } + } else { + Write-Yellow "Note: UPX setup failed, skipping DLL compression" + } + } + } else { + Build-StaticLibrary $PlatformName $CURRENT_GOARCH "a" "generic-libtestoptimization-static" $GENERIC_CFLAGS $GENERIC_LDFLAGS + Build-DynamicLibrary $PlatformName $CURRENT_GOARCH "so" "generic-libtestoptimization-dynamic" $GENERIC_CFLAGS $GENERIC_LDFLAGS "strip" + } + } +} + +# Return to original location +Set-Location $originalLocation + +Write-Blue "--- Build Summary ---" +Write-Green "✓ All libraries built successfully!" +Write-Yellow "Output directory: $ABS_OUTPUT_DIR" +Write-Yellow "Available builds:" + +# List output directories +Get-ChildItem $ABS_OUTPUT_DIR -Directory | ForEach-Object { + Write-Host " • $($_.Name): $($_.FullName)/" +} + +Write-Blue "=== Build completed successfully! ===" \ No newline at end of file From 44bf64b6289909539f573d23c7926a2a2c807ee2 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:44:44 +0200 Subject: [PATCH 07/13] add help test for the bash version --- build/localdev.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/build/localdev.sh b/build/localdev.sh index 77f4540..63aea8d 100755 --- a/build/localdev.sh +++ b/build/localdev.sh @@ -2,6 +2,54 @@ set -e # Exit on any error +# Function to display help +show_help() { + cat << EOF +Test Optimization Native Library - Local Development Build Script + +This script builds the native library for local development by: +1. Setting up the dd-trace-go repository +2. Copying build and native files +3. Building platform-specific libraries +4. Applying platform-specific optimizations + +Usage: + ./localdev.sh [OPTIONS] + +Options: + -h, --help Show this help message and exit + +Requirements: + - Go 1.24 or later + - Git + - Platform-specific build tools (MinGW for Windows, etc.) + +Output: + All build artifacts will be placed in ../dev-output/ + +Examples: + ./localdev.sh # Build for current platform + ./localdev.sh --help # Show this help + +EOF +} + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + -h|--help|-\?) + show_help + exit 0 + ;; + *) + echo "Unknown option: $1" + echo "Use --help for usage information." + exit 1 + ;; + esac + shift +done + # Configuration DEV_DIR="../dev" OUTPUT_DIR="../dev-output" From cafc28ca0ad819d7a606978d3d0f6059c1a6652b Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 00:56:46 +0200 Subject: [PATCH 08/13] Adding the TEST_OPTIMIZATION_DEV_MODE switch to the rust sdk to use the localdev outputs --- sdks/rust/test-optimization-sdk/build.rs | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/sdks/rust/test-optimization-sdk/build.rs b/sdks/rust/test-optimization-sdk/build.rs index f9f6290..e8b97ec 100644 --- a/sdks/rust/test-optimization-sdk/build.rs +++ b/sdks/rust/test-optimization-sdk/build.rs @@ -11,6 +11,7 @@ use ureq::AsSendBody; const TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL: &str = "TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL"; const TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: &str = "TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH"; +const TEST_OPTIMIZATION_DEV_MODE: &str = "TEST_OPTIMIZATION_DEV_MODE"; const TEST_OPTIMIZATION_DOWNLOAD_URL_FORMAT: &str = "https://github.com/DataDog/test-optimization-native/releases/download/v0.0.3-preview/"; fn main() { @@ -25,6 +26,13 @@ fn main() { format!("{}-{}-libtestoptimization-static.zip", platform, arch) }; + // Check for dev mode first (highest priority) + if env::var(TEST_OPTIMIZATION_DEV_MODE).is_ok() { + link_from_dev_output(platform, arch); + other_links(&target); + return; + } + // Check for custom native library search path if let Ok(search_path) = env::var(TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH) { link_from_search_path(platform, &lib_name, &search_path); @@ -134,6 +142,37 @@ fn link_from_search_path(platform: &str, lib_name: &str, search_path: &str) { } } +fn link_from_dev_output(platform: &str, arch: &str) { + // Construct the dev output folder name based on platform and arch + let folder_name = if platform == "macos" { + format!("{}-libtestoptimization-static", platform) + } else { + format!("{}-{}-libtestoptimization-static", platform, arch) + }; + + // The dev-output directory is relative to the repo root + // build.rs is in sdks/rust/test-optimization-sdk/, so we go up 3 levels + let dev_output_path = Path::new("../../../dev-output").join(&folder_name); + + // Check if the library files exist + let has_library = match platform { + "windows" => dev_output_path.join("testoptimization.lib").exists(), + "linux" => dev_output_path.join("libtestoptimization.a").exists(), + "macos" => dev_output_path.join("libtestoptimization.a").exists(), + _ => false, + }; + + if has_library { + println!("cargo:warning=Using dev mode native library from: {}", dev_output_path.display()); + println!("cargo:rustc-link-search=native={}", dev_output_path.display()); + println!("cargo:rustc-link-lib=static=testoptimization"); + } else { + println!("cargo:warning=Dev mode enabled but library not found at: {}", dev_output_path.display()); + println!("cargo:warning=Please run the localdev.sh script to build the native libraries first"); + process::exit(1); + } +} + fn other_links(target: &str) { if !target.contains("windows") { // Link to the dynamic dependency From 50e8695fa2f1b4e9c3a226a0712cf6b095a0bf83 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 01:00:58 +0200 Subject: [PATCH 09/13] add dev mode to the python sdk --- .../src/test_optimization_sdk/constants.py | 3 +- .../src/test_optimization_sdk/native_lib.py | 73 ++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/constants.py b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/constants.py index 7704bdc..fe1f899 100644 --- a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/constants.py +++ b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/constants.py @@ -4,5 +4,6 @@ # Environment variable constants TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL = "TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL" -TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH = "TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH" +TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH = "TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH" +TEST_OPTIMIZATION_DEV_MODE = "TEST_OPTIMIZATION_DEV_MODE" TEST_OPTIMIZATION_DOWNLOAD_URL_FORMAT = "https://github.com/DataDog/test-optimization-native/releases/download/v0.0.3-preview/" \ No newline at end of file diff --git a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py index 501fc0e..4183904 100644 --- a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py +++ b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py @@ -4,7 +4,7 @@ import urllib.request import zipfile from pathlib import Path -from .constants import TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL, TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH, TEST_OPTIMIZATION_DOWNLOAD_URL_FORMAT +from .constants import TEST_OPTIMIZATION_SDK_SKIP_NATIVE_INSTALL, TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH, TEST_OPTIMIZATION_DEV_MODE, TEST_OPTIMIZATION_DOWNLOAD_URL_FORMAT def get_library_filename(): """Get the appropriate library filename based on the platform.""" @@ -59,8 +59,79 @@ def download_native_library(): print(f"Failed to extract native library: {e}", file=sys.stderr) sys.exit(1) +def setup_dev_mode_library(): + """Setup the native library from dev-output folder.""" + # Get platform and architecture info + system = platform.system().lower() + machine = platform.machine().lower() + + if system == "darwin": + system = "macos" + elif system == "windows": + system = "windows" + elif system == "linux": + system = "linux" + else: + raise RuntimeError(f"Unsupported platform: {system}") + + if "arm64" in machine or "aarch64" in machine: + arch = "arm64" + else: + arch = "x64" + + # Construct the dev output folder name (dynamic libraries) + if system == "macos": + folder_name = f"{system}-libtestoptimization-dynamic" + lib_filename = "libtestoptimization.dylib" + elif system == "linux": + folder_name = f"{system}-{arch}-libtestoptimization-dynamic" + lib_filename = "libtestoptimization.so" + elif system == "windows": + folder_name = f"{system}-{arch}-libtestoptimization-dynamic" + lib_filename = "testoptimization.dll" + else: + raise RuntimeError(f"Unsupported platform: {system}") + + # The dev-output directory is relative to the repo root + # native_lib.py is in sdks/python/test-optimization-sdk/src/test_optimization_sdk/ + # so we go up 5 levels to reach repo root + current_file = Path(__file__).resolve() + repo_root = current_file.parents[4] # Go up 5 levels + dev_output_path = repo_root / "dev-output" / folder_name + + # Check if the library file exists + lib_file_path = dev_output_path / lib_filename + + if lib_file_path.exists(): + print(f"Using dev mode native library from: {dev_output_path}") + + # Copy the library to our lib directory + package_dir = Path(__file__).parent + lib_dir = package_dir / "lib" + lib_dir.mkdir(exist_ok=True) + + # Copy the library file and header + import shutil + shutil.copy2(lib_file_path, lib_dir / lib_filename) + + # Also copy header file if it exists + header_file = dev_output_path / "libtestoptimization.h" + if header_file.exists(): + shutil.copy2(header_file, lib_dir / "libtestoptimization.h") + + return True + else: + print(f"Dev mode enabled but library not found at: {lib_file_path}", file=sys.stderr) + print("Please run the localdev.sh script to build the native libraries first", file=sys.stderr) + sys.exit(1) + def setup_native_library(): """Setup the native library during package installation.""" + # Check for dev mode first (highest priority) + if os.environ.get(TEST_OPTIMIZATION_DEV_MODE): + setup_dev_mode_library() + return + # Check for custom search path custom_search_path = os.environ.get(TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH) if custom_search_path: From 372e80632d2d7606304911cfe780506af08d5e97 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 01:04:43 +0200 Subject: [PATCH 10/13] fix --- .../src/test_optimization_sdk/native_lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py index 4183904..057cfec 100644 --- a/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py +++ b/sdks/python/test-optimization-sdk/src/test_optimization_sdk/native_lib.py @@ -96,7 +96,7 @@ def setup_dev_mode_library(): # native_lib.py is in sdks/python/test-optimization-sdk/src/test_optimization_sdk/ # so we go up 5 levels to reach repo root current_file = Path(__file__).resolve() - repo_root = current_file.parents[4] # Go up 5 levels + repo_root = current_file.parents[5] # Go up 5 levels: test_optimization_sdk -> src -> test-optimization-sdk -> python -> sdks -> repo_root dev_output_path = repo_root / "dev-output" / folder_name # Check if the library file exists From 6a97199e999f1f1f3e359299314fdeb397624c5c Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 09:14:54 +0200 Subject: [PATCH 11/13] Adds the ldevcargo.sh script --- sdks/rust/test-optimization-sdk/ldevcargo.sh | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 sdks/rust/test-optimization-sdk/ldevcargo.sh diff --git a/sdks/rust/test-optimization-sdk/ldevcargo.sh b/sdks/rust/test-optimization-sdk/ldevcargo.sh new file mode 100755 index 0000000..19220d1 --- /dev/null +++ b/sdks/rust/test-optimization-sdk/ldevcargo.sh @@ -0,0 +1,81 @@ +#!/bin/bash +set -e # Exit on any error + +# Colors for output +BLUE='\033[0;34m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Function to display help +show_help() { + cat << EOF +Rust SDK Local Development Build Script + +This script builds native libraries and runs cargo commands with dev mode enabled. + +Usage: + ./ldevcargo.sh [OPTIONS] [CARGO_ARGS...] + +Options: + -h, --help Show this help message and exit + -sn, --skip-native Skip building native libraries (use existing builds) + +Examples: + ./ldevcargo.sh build # Build native libs + cargo build + ./ldevcargo.sh test # Build native libs + cargo test + ./ldevcargo.sh -sn check # Skip native build, run cargo check + ./ldevcargo.sh --skip-native clippy # Skip native build, run cargo clippy + ./ldevcargo.sh build --release # Build native libs + cargo build --release + +Notes: + - Native libraries are built using ../../../build/localdev.sh + - TEST_OPTIMIZATION_DEV_MODE=1 is automatically set + - The --skip-native/-sn flag is filtered out before passing args to cargo + +EOF +} + +# Parse arguments to check for --skip-native and help +SKIP_NATIVE=false +CARGO_ARGS=() + +for arg in "$@"; do + case $arg in + -h|--help) + show_help + exit 0 + ;; + -sn|--skip-native) + SKIP_NATIVE=true + ;; + *) + CARGO_ARGS+=("$arg") + ;; + esac +done + +echo -e "${BLUE}=== Rust SDK Local Development Build ===${NC}" + +# Run localdev.sh first to build native libraries (unless skipped) +if [ "$SKIP_NATIVE" = true ]; then + echo -e "${YELLOW}Skipping native library build (--skip-native specified)${NC}" +else + LOCALDEV_SCRIPT="../../../build/localdev.sh" + + if [ -f "$LOCALDEV_SCRIPT" ]; then + echo -e "${BLUE}Building native libraries first...${NC}" + cd ../../../build + ./localdev.sh + cd - > /dev/null + echo -e "${GREEN}✓ Native libraries built${NC}" + else + echo -e "\033[0;31mError: localdev.sh not found at $LOCALDEV_SCRIPT${NC}" + exit 1 + fi +fi + +echo -e "${BLUE}Running cargo with dev mode...${NC}" +export TEST_OPTIMIZATION_DEV_MODE=1 + +cargo "${CARGO_ARGS[@]}" \ No newline at end of file From 825d7a9d00b258140bcb4ccee1e630f088b424f9 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 09:23:04 +0200 Subject: [PATCH 12/13] adds the powershell version of ldevcargo --- sdks/rust/test-optimization-sdk/ldevcargo.cmd | 44 +++++++ sdks/rust/test-optimization-sdk/ldevcargo.ps1 | 107 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 sdks/rust/test-optimization-sdk/ldevcargo.cmd create mode 100644 sdks/rust/test-optimization-sdk/ldevcargo.ps1 diff --git a/sdks/rust/test-optimization-sdk/ldevcargo.cmd b/sdks/rust/test-optimization-sdk/ldevcargo.cmd new file mode 100644 index 0000000..04f1d92 --- /dev/null +++ b/sdks/rust/test-optimization-sdk/ldevcargo.cmd @@ -0,0 +1,44 @@ +@echo off +setlocal enabledelayedexpansion + +REM Check if this is a help request +if "%~1"=="-h" goto :show_help +if "%~1"=="--help" goto :show_help + +REM Check if PowerShell is available +where powershell >nul 2>&1 +if errorlevel 1 ( + echo Error: PowerShell is not available on this system + echo Please install PowerShell or use a different method to run cargo commands + exit /b 1 +) + +REM Run the PowerShell script with execution policy bypass +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0ldevcargo.ps1" %* +exit /b %errorlevel% + +:show_help +echo Rust SDK Local Development Build Script - CMD Wrapper +echo. +echo This wrapper runs the PowerShell version of ldevcargo with execution policy bypass. +echo. +echo Usage: +echo ldevcargo.cmd [OPTIONS] [CARGO_ARGS...] +echo. +echo Options: +echo -h, --help Show this help message and exit +echo -sn, --skip-native Skip building native libraries (use existing builds) +echo. +echo Examples: +echo ldevcargo.cmd build # Build native libs + cargo build +echo ldevcargo.cmd test # Build native libs + cargo test +echo ldevcargo.cmd -sn check # Skip native build, run cargo check +echo ldevcargo.cmd --skip-native clippy # Skip native build, run cargo clippy +echo ldevcargo.cmd build --release # Build native libs + cargo build --release +echo. +echo Notes: +echo - Requires PowerShell to be installed +echo - Runs with -NoProfile -ExecutionPolicy Bypass for clean execution +echo - All arguments are passed to the PowerShell script +echo. +exit /b 0 \ No newline at end of file diff --git a/sdks/rust/test-optimization-sdk/ldevcargo.ps1 b/sdks/rust/test-optimization-sdk/ldevcargo.ps1 new file mode 100644 index 0000000..b2d7a20 --- /dev/null +++ b/sdks/rust/test-optimization-sdk/ldevcargo.ps1 @@ -0,0 +1,107 @@ +# PowerShell Local Development Cargo Script for Rust SDK +param( + [Parameter(ValueFromRemainingArguments = $true)] + [string[]]$RemainingArgs = @() +) + +# Enable strict mode and stop on errors +$ErrorActionPreference = "Stop" +Set-StrictMode -Version Latest + +# Colors for output (using Write-Host with colors) +function Write-ColorOutput { + param( + [string]$Message, + [string]$Color = "White" + ) + Write-Host $Message -ForegroundColor $Color +} + +function Write-Blue { param([string]$Message) Write-ColorOutput $Message "Blue" } +function Write-Green { param([string]$Message) Write-ColorOutput $Message "Green" } +function Write-Yellow { param([string]$Message) Write-ColorOutput $Message "Yellow" } +function Write-Red { param([string]$Message) Write-ColorOutput $Message "Red" } + +# Function to display help +function Show-Help { + Write-Host @" +Rust SDK Local Development Build Script + +This script builds native libraries and runs cargo commands with dev mode enabled. + +Usage: + .\ldevcargo.ps1 [OPTIONS] [CARGO_ARGS...] + pwsh -NoProfile .\ldevcargo.ps1 [OPTIONS] [CARGO_ARGS...] (recommended) + +Options: + -h, --help Show this help message and exit + -sn, --skip-native Skip building native libraries (use existing builds) + +Examples: + .\ldevcargo.ps1 build # Build native libs + cargo build + .\ldevcargo.ps1 test # Build native libs + cargo test + .\ldevcargo.ps1 -sn check # Skip native build, run cargo check + .\ldevcargo.ps1 --skip-native clippy # Skip native build, run cargo clippy + .\ldevcargo.ps1 build --release # Build native libs + cargo build --release + +Notes: + - Native libraries are built using ../../../build/localdev.ps1 + - TEST_OPTIMIZATION_DEV_MODE=1 is automatically set + - The --skip-native/-sn flag is filtered out before passing args to cargo + - Use 'pwsh -NoProfile' to avoid PowerShell profile interference + +"@ +} + +# Parse arguments to check for --skip-native and help +$SkipNative = $false +$CargoArgs = @() + +foreach ($arg in $RemainingArgs) { + switch ($arg) { + { $_ -in @("-h", "--help") } { + Show-Help + exit 0 + } + { $_ -in @("-sn", "--skip-native") } { + $SkipNative = $true + } + default { + $CargoArgs += $arg + } + } +} + +Write-Blue "=== Rust SDK Local Development Build ===" + +# Run localdev.ps1 first to build native libraries (unless skipped) +if ($SkipNative) { + Write-Yellow "Skipping native library build (--skip-native specified)" +} else { + $LocaldevScript = "../../../build/localdev.ps1" + + if (Test-Path $LocaldevScript) { + Write-Blue "Building native libraries first..." + $currentLocation = Get-Location + Set-Location "../../../build" + try { + & .\localdev.ps1 + Write-Green "✓ Native libraries built" + } finally { + Set-Location $currentLocation + } + } else { + Write-Red "Error: localdev.ps1 not found at $LocaldevScript" + exit 1 + } +} + +Write-Blue "Running cargo with dev mode..." +$env:TEST_OPTIMIZATION_DEV_MODE = "1" + +# Run cargo with filtered arguments +if ($CargoArgs.Count -gt 0) { + & cargo @CargoArgs +} else { + & cargo +} \ No newline at end of file From 07920ab6f619c2de0d9215bb197ddd94981e9ce5 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 16 Jul 2025 09:39:47 +0200 Subject: [PATCH 13/13] add debug flag --- .github/workflows/rust-sdk-tests.yml | 6 ++++++ sdks/rust/test-optimization-sdk/Dockerfile | 2 ++ 2 files changed, 8 insertions(+) diff --git a/.github/workflows/rust-sdk-tests.yml b/.github/workflows/rust-sdk-tests.yml index b72d951..b9386e6 100644 --- a/.github/workflows/rust-sdk-tests.yml +++ b/.github/workflows/rust-sdk-tests.yml @@ -66,6 +66,7 @@ jobs: env: TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: ${{ github.workspace }}/build_artifacts JOB_DISPLAY_NAME: Run Rust SDK Tests on Linux AMD64 with Docker + DD_TRACE_DEBUG: 1 steps: - name: Create Check @@ -107,6 +108,7 @@ jobs: env: TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: ${{ github.workspace }}/build_artifacts JOB_DISPLAY_NAME: Run Rust SDK Tests on Linux AMD64 + DD_TRACE_DEBUG: 1 steps: - name: Create Check @@ -153,6 +155,8 @@ jobs: env: TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: ${{ github.workspace }}/build_artifacts JOB_DISPLAY_NAME: Run Rust SDK Tests on Linux ARM64 + DD_TRACE_DEBUG: 1 + steps: - name: Create Check id: create_check @@ -199,6 +203,7 @@ jobs: env: TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: ${{ github.workspace }}/build_artifacts JOB_DISPLAY_NAME: Run Rust SDK Tests on macOS + DD_TRACE_DEBUG: 1 steps: - name: Create Check @@ -249,6 +254,7 @@ jobs: env: TEST_OPTIMIZATION_SDK_NATIVE_SEARCH_PATH: ${{ github.workspace }}/build_artifacts JOB_DISPLAY_NAME: Run Rust SDK Tests on Windows + DD_TRACE_DEBUG: 1 steps: - name: Create Check diff --git a/sdks/rust/test-optimization-sdk/Dockerfile b/sdks/rust/test-optimization-sdk/Dockerfile index 70ce80d..0eb8b12 100644 --- a/sdks/rust/test-optimization-sdk/Dockerfile +++ b/sdks/rust/test-optimization-sdk/Dockerfile @@ -13,5 +13,7 @@ COPY Cargo.toml Cargo.lock ./ COPY src ./src COPY build.rs ./ +ENV DD_TRACE_DEBUG=1 + # Run the tests CMD ["cargo", "test", "--", "--nocapture"] \ No newline at end of file