CMake setup

This commit is contained in:
York Jasper Niebuhr 2025-10-07 23:04:22 +02:00
commit 2a5cb3edc3
6 changed files with 79 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

5
CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(SelfPatchSLR LANGUAGES C CXX)
add_subdirectory(plugin)
add_subdirectory(subject)

9
plugin/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
add_library(selfpatch-slr SHARED main.cpp)
set_target_properties(selfpatch-slr PROPERTIES PREFIX "")
target_compile_definitions(selfpatch-slr PRIVATE _GNU_SOURCE)
message(STATUS "C compiler: ${CMAKE_C_COMPILER}")
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=plugin OUTPUT_VARIABLE GCC_PLUGIN_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "GCC plugin path: ${GCC_PLUGIN_PATH}")
target_include_directories(selfpatch-slr PRIVATE ${GCC_PLUGIN_PATH}/include)

31
plugin/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include "gcc-plugin.h"
#include "plugin-version.h"
#include "tree.h"
int plugin_is_GPL_compatible;
static tree handle_slr_attr(tree* node, tree name, tree args, int flags, bool* no_add_attrs) {
if (!node)
return NULL_TREE;
std::cout << "SLR attribute found!" << std::endl;
return NULL_TREE;
}
static struct attribute_spec slr_attr = { "slr", 0, 0, false, false, false, false, handle_slr_attr, NULL };
void register_attributes(void* event_data, void* data) {
register_attribute(&slr_attr);
}
int plugin_init(struct plugin_name_args* plugin_info, struct plugin_gcc_version* version) {
if (!plugin_default_version_check(version, &gcc_version)) {
std::cerr << "This plugin is for GCC version " << GCCPLUGIN_VERSION_MAJOR << "." << GCCPLUGIN_VERSION_MINOR << std::endl;
return 1;
}
register_callback(plugin_info->base_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
return 0;
}

3
subject/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
add_executable(subject main.c)
add_dependencies(subject selfpatch-slr)
target_compile_options(subject PRIVATE "-fplugin=$<TARGET_FILE:selfpatch-slr>")

30
subject/main.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stddef.h>
#define container_of(ptr, type, member) ({ \
const typeof(((type*)0)->member)* __mptr = (ptr); \
(type*)((char*)__mptr - offsetof(type, member)); })
struct A;
struct B;
struct A {
char m0;
int m1;
struct B* m2;
};
struct B {
int m0;
char m1;
short m2;
struct A* m3;
float m4;
struct A m5;
struct B* m6;
} __attribute__((slr));
int main(int argc, char** argv) {
// TODO
return 0;
}