Skip to content

Commit

Permalink
Merge pull request #14 from bbannier/t/cmake
Browse files Browse the repository at this point in the history
Add CMake setup, test and CI for CC library
  • Loading branch information
milahu authored Sep 8, 2024
2 parents 32d3641 + 11ecae4 commit 6eeaa5c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 49 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/cc-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test CC library

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v2
with:
version: "18"
- run: cmake cc/src -B build
- run: cmake --build build
- run: ctest --test-dir build/

33 changes: 33 additions & 0 deletions cc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.14)
project(gnumake-tokenpool LANGUAGES CXX)

add_library(
gnumake-tokenpool
tokenpool-gnu-make-posix.cc
# tokenpool-gnu-make-win32.cc # TODO: Add this file iff building on win32.
tokenpool-gnu-make.cc)
target_include_directories(gnumake-tokenpool PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

option(GNUMAKE_TOKENPOOL_BUILD_TESTS "build gnumake-tokenpool tests" ON)

if (GNUMAKE_TOKENPOOL_BUILD_TESTS)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/releases/download/v1.15.2/googletest-1.15.2.tar.gz)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_executable(gnumake-tokenpool-test tokenpool_test.cc)
target_link_libraries(gnumake-tokenpool-test gnumake-tokenpool gtest_main)

enable_testing()

include(GoogleTest)
gtest_discover_tests(gnumake-tokenpool-test)
endif ()
34 changes: 1 addition & 33 deletions cc/src/tokenpool-gnu-make-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tokenpool-gnu-make.h"
#include "tokenpool-gnu-make-posix.h"

#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// TokenPool implementation for GNU make jobserver - POSIX implementation
// (http://make.mad-scientist.net/papers/jobserver-implementation/)
struct GNUmakeTokenPoolPosix : public GNUmakeTokenPool {
GNUmakeTokenPoolPosix();
virtual ~GNUmakeTokenPoolPosix();

virtual int GetMonitorFd();

virtual const char* GetEnv(const char* name) { return getenv(name); }
virtual bool SetEnv(const char* name, const char* value) {
return setenv(name, value, 1) == 0;
}
virtual bool ParseAuth(const char* jobserver);
virtual bool CreatePool(int parallelism, std::string* auth);
virtual int AcquireToken();
virtual bool ReleaseToken(int token);

private:
int rfd_;
int wfd_;

struct sigaction old_act_;
bool restore_;

static int dup_rfd_;
static void CloseDupRfd(int signum);

bool CheckFd(int fd);
bool SetAlarmHandler();
};

GNUmakeTokenPoolPosix::GNUmakeTokenPoolPosix() : rfd_(-1), wfd_(-1), restore_(false) {
}

Expand Down
48 changes: 48 additions & 0 deletions cc/src/tokenpool-gnu-make-posix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2016-2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tokenpool-gnu-make.h"

#include <signal.h>

// TokenPool implementation for GNU make jobserver - POSIX implementation
// (http://make.mad-scientist.net/papers/jobserver-implementation/)
struct GNUmakeTokenPoolPosix : public GNUmakeTokenPool {
GNUmakeTokenPoolPosix();
virtual ~GNUmakeTokenPoolPosix();

virtual int GetMonitorFd();

virtual const char* GetEnv(const char* name) { return getenv(name); }
virtual bool SetEnv(const char* name, const char* value) {
return setenv(name, value, 1) == 0;
}
virtual bool ParseAuth(const char* jobserver);
virtual bool CreatePool(int parallelism, std::string* auth);
virtual int AcquireToken();
virtual bool ReleaseToken(int token);

private:
int rfd_;
int wfd_;

struct sigaction old_act_;
bool restore_;

static int dup_rfd_;
static void CloseDupRfd(int signum);

bool CheckFd(int fd);
bool SetAlarmHandler();
};
32 changes: 16 additions & 16 deletions cc/src/tokenpool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "tokenpool.h"

#include "test.h"
#include "gtest/gtest.h"

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -172,15 +172,15 @@ TEST_F(TokenPoolTest, SuccessfulOldSetup) {
// GNUmake <= 4.1
CreatePool(AUTH_FORMAT("--jobserver-fds"));

EXPECT_NE(NULL, tokens_);
EXPECT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);
}

TEST_F(TokenPoolTest, SuccessfulNewSetup) {
// GNUmake => 4.2
CreateDefaultPool();

EXPECT_NE(NULL, tokens_);
EXPECT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);
}

Expand All @@ -194,7 +194,7 @@ TEST_F(TokenPoolTest, IgnoreWithJN) {
TEST_F(TokenPoolTest, HonorLN) {
CreatePool(AUTH_FORMAT("-l9 --jobserver-auth"));

EXPECT_NE(NULL, tokens_);
EXPECT_TRUE(tokens_);
EXPECT_EQ(9.0, load_avg_);
}

Expand All @@ -203,14 +203,14 @@ TEST_F(TokenPoolTest, SemaphoreNotFound) {
semaphore_name_ = SEMAPHORE_NAME "_foobar";
CreateDefaultPool();

EXPECT_EQ(NULL, tokens_);
EXPECT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);
}

TEST_F(TokenPoolTest, TokenIsAvailable) {
CreateDefaultPool();

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);

EXPECT_TRUE(tokens_->TokenIsAvailable((ULONG_PTR)tokens_));
Expand All @@ -219,7 +219,7 @@ TEST_F(TokenPoolTest, TokenIsAvailable) {
TEST_F(TokenPoolTest, MonitorFD) {
CreateDefaultPool();

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);

EXPECT_EQ(fds_[0], tokens_->GetMonitorFd());
Expand All @@ -229,7 +229,7 @@ TEST_F(TokenPoolTest, MonitorFD) {
TEST_F(TokenPoolTest, ImplicitToken) {
CreateDefaultPool();

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);

EXPECT_TRUE(tokens_->Acquire());
Expand All @@ -242,7 +242,7 @@ TEST_F(TokenPoolTest, ImplicitToken) {
TEST_F(TokenPoolTest, TwoTokens) {
CreateDefaultPool();

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);

// implicit token
Expand Down Expand Up @@ -286,7 +286,7 @@ TEST_F(TokenPoolTest, TwoTokens) {
TEST_F(TokenPoolTest, Clear) {
CreateDefaultPool();

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);
EXPECT_EQ(kLoadAverageDefault, load_avg_);

// implicit token
Expand Down Expand Up @@ -328,17 +328,17 @@ TEST_F(TokenPoolTest, Clear) {
TEST_F(TokenPoolTest, NoPoolForSerialBuild) {
CreateMaster(1);

EXPECT_EQ(NULL, tokens_);
EXPECT_FALSE(tokens_);
}

TEST_F(TokenPoolTest, MasterNoLoadAvg) {
// kLoadAverageDefault <= 0.0f -> no load averaging
CreateMaster(2);

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);

const char *env = ENVIRONMENT_GET();
ASSERT_NE(NULL, env);
ASSERT_TRUE(env);

EXPECT_EQ(env, strstr(env, "--jobserver-auth="));
EXPECT_EQ(NULL, strstr(env, " -l"));
Expand All @@ -350,13 +350,13 @@ TEST_F(TokenPoolTest, MasterWithLoadAvg) {
load_avg_ = 3.1415f;
CreateMaster(3);

ASSERT_NE(NULL, tokens_);
ASSERT_TRUE(tokens_);

const char *env = ENVIRONMENT_GET();
ASSERT_NE(NULL, env);
ASSERT_TRUE(env);

EXPECT_EQ(env, strstr(env, "--jobserver-auth="));
EXPECT_NE(NULL, strstr(env, " -l3.1415"));
EXPECT_TRUE(strstr(env, " -l3.1415"));

CheckTokens(env, 3);
}

0 comments on commit 6eeaa5c

Please sign in to comment.