From 78f64b21e66480bf81a6152f0f40ab8d523150e9 Mon Sep 17 00:00:00 2001 From: Kai Kosades Date: Wed, 11 Oct 2023 22:13:39 +0300 Subject: [PATCH] Switched to single build system CMake --- CMakeLists.txt | 72 ++++++++++++++++++++++++++++++++++++++++++ Makefile | 53 ------------------------------- VERSION | 1 - example/CMakeLists.txt | 20 ++++++++++++ example/Makefile | 33 ------------------- example/meson.build | 15 --------- include/meson.build | 7 ---- meson.build | 44 -------------------------- setup.py | 38 ---------------------- src/meson.build | 54 ------------------------------- tests/meson.build | 14 -------- 11 files changed, 92 insertions(+), 259 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 Makefile delete mode 100644 VERSION create mode 100644 example/CMakeLists.txt delete mode 100644 example/Makefile delete mode 100644 example/meson.build delete mode 100644 include/meson.build delete mode 100644 meson.build delete mode 100644 setup.py delete mode 100644 src/meson.build delete mode 100644 tests/meson.build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e58dd1f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,72 @@ +cmake_minimum_required(VERSION 3.22) + +project(libi2c + VERSION 0.2.1 + LANGUAGES C CXX +) + +option(PYTHON_BINDINGS "Enable Python bindings compilation" OFF) +option(BUILD_EXAMPLES "Build library usage examples" OFF) + +if(NOT LINUX) + message(FATAL_ERROR "Non Linux build is not supported") +endif() + +add_library(libi2c + src/i2c.c +) +target_include_directories(libi2c + PUBLIC + include/ +) +target_compile_definitions(libi2c + PUBLIC + "LIBI2C_VERSION=\"${CMAKE_PROJECT_VERSION}\"" +) +target_compile_options(libi2c + PRIVATE + -Wall -Wextra +) +set_target_properties(libi2c + PROPERTIES + PREFIX "" +) + +install(TARGETS libi2c) +install(DIRECTORY include/ + TYPE INCLUDE +) + +if(PYTHON_BINDINGS) + find_package(Python COMPONENTS Interpreter Development) + + add_library(pylibi2c SHARED + src/pyi2c.c + ) + target_link_libraries(pylibi2c + PUBLIC + Python::Python + libi2c + ) + set_target_properties(pylibi2c + PROPERTIES + PREFIX "" + ) + install(TARGETS pylibi2c DESTINATION "${Python_SITEARCH}/") + + enable_testing() + add_test(NAME python_bindings_test + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND ${Python_EXECUTABLE} "${CMAKE_SOURCE_DIR}/tests/test_pylibi2c.py" + ) + set_property(TEST python_bindings_test + PROPERTY + ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}" + ) +endif() + +if(BUILD_EXAMPLES) + add_subdirectory(example) +endif() + +include(CPack) diff --git a/Makefile b/Makefile deleted file mode 100644 index f0d043b..0000000 --- a/Makefile +++ /dev/null @@ -1,53 +0,0 @@ -PYTHON = python -CC = $(CROSS)gcc -AR = $(CROSS)ar -VERSION=$(shell head -n 1 VERSION) -INCDIR = include -CFLAGS = -Wall -I$(INCDIR) -Wextra -g -fPIC -DLIBI2C_VERSION="$(VERSION)" -LDSHFLAGS = -rdynamic -shared -ARFLAGS = rcv -CODE_STYLE = astyle --align-pointer=name --align-reference=name --suffix=none --break-blocks --pad-oper --pad-header --break-blocks --keep-one-line-blocks --indent-switches --indent=spaces - -SOURCES=$(filter-out src/pyi2c.c, $(wildcard src/*.c)) -HEADERS=$(wildcard $(INCDIR)/i2c/*.h) -OBJECTS=$(SOURCES:.c=.o) -TARGETS = libi2c.a libi2c.so pylibi2c.so - -.PHONY:all clean example test install help style -.SILENT: clean - -all:$(TARGETS) example - -clean: - make -C example clean - find . -name "*.o" | xargs rm -f - $(RM) *.o *.so *~ a.out depend $(TARGETS) build -rf - -help: - $(PYTHON) help.py - -test: - $(PYTHON) -m unittest discover tests - -style: - @find -regex '.*/.*\.\(c\|cpp\|h\)$$' | xargs $(CODE_STYLE) - -install: - $(PYTHON) setup.py install - -example:$(TARGETS) - make -C $@ - -libi2c.a:$(OBJECTS) - $(AR) $(ARFLAGS) $@ $^ - -libi2c.so:$(OBJECTS) - $(CC) $(LDSHFLAGS) -o $@ $^ - -pylibi2c.so:$(OBJECTS) - $(PYTHON) setup.py build_ext --inplace - -depend:$(SOURCES) $(HEADERS) src/pyi2c.c - $(CC) $(CFLAGS) -MM $^ > $@ - --include depend diff --git a/VERSION b/VERSION deleted file mode 100644 index 7dff5b8..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.2.1 \ No newline at end of file diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..1e39ea9 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.22) + +project(libi2c_examples) + +add_executable(i2c_tools + i2c_tools.c +) +target_link_libraries(i2c_tools + PUBLIC + libi2c +) + +add_executable(i2c_without_internal_address + i2c_without_internal_address.c +) +target_link_libraries(i2c_without_internal_address + PUBLIC + libi2c +) +install(TARGETS i2c_tools i2c_without_internal_address) diff --git a/example/Makefile b/example/Makefile deleted file mode 100644 index d835bc3..0000000 --- a/example/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -CC = $(CROSS)gcc -AR = $(CROSS)ar -CFLAGS = -Wall -g -LDSHFLAGS = -rdynamic -shared -ARFLAGS = rcv -CFLAGS += -I../include -LDFLAGS = -L.. -li2c -Wl,-R -Wl,.. - -OBJDIR=../objs -SOURCES = $(wildcard *.c) $(wildcard *.cpp) -TARGETS = $(foreach src, $(SOURCES), $(basename $(src))) - -.PHONY:all clean objdir -.SILENT:clean - -all:objdir $(TARGETS) - -objdir: - @mkdir -p $(OBJDIR) - -clean: - $(RM) *.o a.out depend $(TARGETS) $(OBJDIR) -rf - -i2c_tools: i2c_tools.o - $(CC) $(CFLAGS) -o $(OBJDIR)/$@ $^ $(LDFLAGS) - -i2c_without_internal_address: i2c_without_internal_address.o - $(CC) $(CFLAGS) -o $(OBJDIR)/$@ $^ $(LDFLAGS) - -depend:$(wildcard *.h *.c) - $(CC) $(CFLAGS) -MM $^ > $@ - --include depend diff --git a/example/meson.build b/example/meson.build deleted file mode 100644 index c44338f..0000000 --- a/example/meson.build +++ /dev/null @@ -1,15 +0,0 @@ -examples = [ - 'i2c_tools', - 'i2c_without_internal_address', -] - -foreach example: examples - exe = executable(example, example + '.c', - link_with: libi2c, - include_directories: i2c_incdir, - c_args: [ - cflags, - '-D_DEFAULT_SOURCE', - ], - ) -endforeach \ No newline at end of file diff --git a/include/meson.build b/include/meson.build deleted file mode 100644 index d258ebd..0000000 --- a/include/meson.build +++ /dev/null @@ -1,7 +0,0 @@ -i2c_incdir = include_directories('.') - -# public headers -install_headers(['i2c/i2c.h'], - # i2c.h can clash with linux/i2c.h, so we put it in a subdir - subdir: meson.project_name(), -) \ No newline at end of file diff --git a/meson.build b/meson.build deleted file mode 100644 index c6979f1..0000000 --- a/meson.build +++ /dev/null @@ -1,44 +0,0 @@ -project('i2c', ['c'], - version: run_command( - 'head', '-n', '1', files('VERSION'), - ).stdout().strip(), - meson_version: '>= 0.56.0', - license: 'MIT', - default_options: [ - 'c_std=c11', - 'warning_level=2', # -Wall, -Wextra - 'werror=true', # -Werror - ], -) - -# the c++ compiler object -cc = meson.get_compiler('c') - -# global cflags (public, included in subprojects and pkg-config) -cflags = ['-DLIBI2C_VERSION="' + meson.project_version() + '"'] - -# per compiler cflags, if desired -if cc.get_id() == 'clang' - cflags += [] -else - cflags += [] -endif - -# split the project version -proj_ver = meson.project_version().split('.') -# version minus patch revision -proj_ver_short = proj_ver[0] + '.' + proj_ver[1] -proj_ver_major = proj_ver[0] -proj_ver_minor = proj_ver[1] -proj_ver_patch = proj_ver[2] - -# all of this is used by configure_file on i2c_config.h.in -proj_description = 'Linux userspace i2c operation library' -proj_url = 'https://github.com/amaork/libi2c' -binary_package = 'libi2c' -origin = 'github' - -subdir('include') -subdir('src') -subdir('example') -subdir('tests') \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100644 index ab61535..0000000 --- a/setup.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -""" -setup.py file for pylibi2c -""" - -import os -from distutils.core import setup, Extension - -THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -INC_DIR = os.path.join(THIS_DIR, 'include') -VERSION = open('VERSION').read().strip() - -pylibi2c_module = Extension('pylibi2c', - sources=['src/i2c.c', 'src/pyi2c.c'], - extra_compile_args=['-DLIBI2C_VERSION="' + VERSION + '"'], - include_dirs=[INC_DIR], -) - -setup( - name='pylibi2c', - version=VERSION, - license='MIT', - author='Amaork', - author_email="amaork@gmail.com", - url='https://github.com/amaork/libi2c', - description="Linux userspace i2c operation library", - long_description=open('README.md').read(), - ext_modules=[pylibi2c_module], - py_modules=["pylibi2c"], - classifiers=[ - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 3', - ], -) - - diff --git a/src/meson.build b/src/meson.build deleted file mode 100644 index 85b2428..0000000 --- a/src/meson.build +++ /dev/null @@ -1,54 +0,0 @@ -# source for core library -i2c_src = [ - 'i2c.c', -] - -# shared and/or static library -libi2c = library(meson.project_name(), i2c_src, - c_args: [ - cflags, - '-D_DEFAULT_SOURCE', - ], - include_directories: i2c_incdir, - install: true, -) - -# dependency for use in subprojects (only) -i2c_dep = declare_dependency( - compile_args: cflags, - include_directories: i2c_incdir, - link_with: libi2c, - version: meson.project_version(), -) - -# pkg-config support -pkg = import('pkgconfig') -pkg.generate(libi2c, - extra_cflags: cflags, -) - -# python module build follows -pymod = import('python') -python_versions = ['python2', 'python3'] -python_testme = [] # python execs and built libraries go in here for testing -python_src = ['pyi2c.c'] -python_path = meson.current_build_dir() # used for tests - -foreach python_version : python_versions - python = pymod.find_installation(python_version, required: false) - if python.found() - # we still need the python-dev package - python_dep = dependency(python_version) - message(python_version + ' module enabled') - # same arguments as `shared_module`, more or less - # https://mesonbuild.com/Python-module.html - pylib = python.extension_module('pylibi2c', python_src, - dependencies: python_dep, - include_directories: i2c_incdir, - link_with: libi2c, - install: true, - c_args: cflags, - ) - python_testme += {'python': python, 'lib': pylib} - endif -endforeach \ No newline at end of file diff --git a/tests/meson.build b/tests/meson.build deleted file mode 100644 index 3840021..0000000 --- a/tests/meson.build +++ /dev/null @@ -1,14 +0,0 @@ -foreach d : python_testme - python = d['python'] - testname = 'python ' + python.language_version() + ' module' - test(testname, python, # testname, executable - args: files('test_pylibi2c.py'), # full path to test.py - depends: d['lib'], # the library that must be built before - workdir: python_path, - protocol: 'exitcode', - env: [ - 'PYTHONPATH=' + python_path, - ], - is_parallel: false, - ) -endforeach \ No newline at end of file