Skip to content

Commit

Permalink
mkm: add script to split files, make client.h and others work with it
Browse files Browse the repository at this point in the history
  • Loading branch information
septract committed Dec 27, 2024
1 parent faaa2ef commit 6fa262f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
11 changes: 9 additions & 2 deletions components/mission_key_management/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#include <stdlib.h>
#include <string.h>

#include "client.h"
#include <stdint.h>

#ifndef CN_ENV
# include <sys/epoll.h>
Expand All @@ -15,6 +14,14 @@
# include <unistd.h>
# include <stdio.h>

#ifdef CN_ENV
# include <sys/types.h>
#endif

//SYSTEM_HEADERS

#include "client.h"

#ifdef CN_ENV
# include "cn_memory.h"
# include "cn_stubs.h"
Expand Down
1 change: 0 additions & 1 deletion components/mission_key_management/client.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <stdint.h>
#include "policy.h"

enum client_state {
Expand Down
1 change: 0 additions & 1 deletion components/mission_key_management/cn_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


// Cerberus puts some POSIX headers under the `posix/` directory.
#include <posix/sys/types.h>
#include "policy.h"

// From `sys/epoll.h`
Expand Down
58 changes: 58 additions & 0 deletions components/mission_key_management/process.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

# Run the C preprocessor on the second half of a file only:
# 1. Split the file at the first occurrence of //SYSTEM_HEADERS
# 2. Run the C preprocessor on the second chunk
# 3. Concatenate the first chunk and preprocessed second chunk
#
# Usage:
# ./process.sh <input-file> [<output-file>]

set -euo pipefail

# Check arguments
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <input-file> [<output-file>]"
exit 1
fi

INPUT_FILE="$1"
OUTBASE="${INPUT_FILE%.c}"
DEFAULT_OUTPUT="${OUTBASE}-out.c"

# If user provided the second argument, use it; otherwise use our default
OUTPUT_FILE="${2:-$DEFAULT_OUTPUT}"

# Create a temporary directory and ensure it's cleaned up on exit
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT

# 1. Split at the first occurrence of /\/\/SYSTEM_HEADERS/
csplit -s -f "${TMP_DIR}/split_" -n 1 "$INPUT_FILE" "/\/\/SYSTEM_HEADERS/"

# 2. Run the C preprocessor on the second chunk
ROOT_DIR="$(pwd)"

# Need to figure out where the cerberus posix headers are
eval "$(opam env)"

# gcc flags, stored in an array for robustness
GCC_FLAGS=(
"-E" "-P" "-CC" "-x" "c"
"--include=${ROOT_DIR}/../include/wars.h"
"-I${ROOT_DIR}/../include"
"-I."
"-I${OPAM_SWITCH_PREFIX}/lib/cerberus/runtime/libc/include/posix"
"-DCN_ENV"
)

# # Print the exact command for debug purposes
# echo "Running gcc command:"
# echo gcc "${GCC_FLAGS[@]}" "${TMP_DIR}/split_1" -o "${TMP_DIR}/split_1_out.c"

gcc "${GCC_FLAGS[@]}" "${TMP_DIR}/split_1" -o "${TMP_DIR}/split_1_out.c"

# 3. Concatenate the first chunk and preprocessed second chunk
cat "${TMP_DIR}/split_0" "${TMP_DIR}/split_1_out.c" > "$OUTPUT_FILE"

echo "Done. Merged output is in: $OUTPUT_FILE"

0 comments on commit 6fa262f

Please sign in to comment.