├── .clang-format ├── .gitignore ├── LICENSE ├── Makefile ├── README.md └── src ├── CMakeLists.txt ├── cmake ├── asan.cmake ├── go.cmake └── toolchains │ ├── host.toolchain │ └── pi.toolchain ├── examples ├── CMakeLists.txt ├── goodprogram │ ├── CMakeLists.txt │ ├── go.mod │ └── go │ │ └── goodprogram │ │ └── main.go ├── leakyprogram │ ├── CMakeLists.txt │ ├── go.mod │ └── go │ │ └── leakyprogram │ │ └── main.go └── segfaultyprogram │ ├── CMakeLists.txt │ ├── go.mod │ └── go │ ├── nullrefprogram │ └── main.go │ └── outofboundsprogram │ └── main.go └── libs ├── CMakeLists.txt ├── common ├── CMakeLists.txt ├── include │ ├── myarray.h │ └── mylog.h └── src │ ├── myarray.c │ └── mylog.c └── stuff ├── CMakeLists.txt ├── include └── stuff.h └── src └── stuff.c /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | # BasedOnStyle: LLVM 4 | AccessModifierOffset: -2 5 | AlignAfterOpenBracket: AlwaysBreak 6 | AlignConsecutiveMacros: false 7 | AlignConsecutiveAssignments: false 8 | AlignConsecutiveDeclarations: false 9 | AlignEscapedNewlines: Right 10 | AlignOperands: true 11 | AlignTrailingComments: true 12 | AllowAllArgumentsOnNextLine: true 13 | AllowAllConstructorInitializersOnNextLine: true 14 | AllowAllParametersOfDeclarationOnNextLine: true 15 | AllowShortBlocksOnASingleLine: Never 16 | AllowShortCaseLabelsOnASingleLine: false 17 | AllowShortFunctionsOnASingleLine: false 18 | AllowShortLambdasOnASingleLine: false 19 | AllowShortIfStatementsOnASingleLine: Never 20 | AllowShortLoopsOnASingleLine: false 21 | AlwaysBreakAfterDefinitionReturnType: None 22 | AlwaysBreakAfterReturnType: None 23 | AlwaysBreakBeforeMultilineStrings: false 24 | AlwaysBreakTemplateDeclarations: MultiLine 25 | BinPackArguments: true 26 | BinPackParameters: true 27 | BraceWrapping: 28 | AfterCaseLabel: false 29 | AfterClass: false 30 | AfterControlStatement: false 31 | AfterEnum: false 32 | AfterFunction: false 33 | AfterNamespace: false 34 | AfterObjCDeclaration: false 35 | AfterStruct: false 36 | AfterUnion: false 37 | AfterExternBlock: false 38 | BeforeCatch: false 39 | BeforeElse: false 40 | IndentBraces: false 41 | SplitEmptyFunction: true 42 | SplitEmptyRecord: true 43 | SplitEmptyNamespace: true 44 | BreakBeforeBinaryOperators: None 45 | BreakBeforeBraces: Allman 46 | BreakBeforeInheritanceComma: false 47 | BreakInheritanceList: BeforeColon 48 | BreakBeforeTernaryOperators: true 49 | BreakConstructorInitializersBeforeComma: false 50 | BreakConstructorInitializers: BeforeColon 51 | BreakAfterJavaFieldAnnotations: false 52 | BreakStringLiterals: true 53 | ColumnLimit: 80 54 | CommentPragmas: '^ IWYU pragma:' 55 | CompactNamespaces: false 56 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 57 | ConstructorInitializerIndentWidth: 4 58 | ContinuationIndentWidth: 4 59 | Cpp11BracedListStyle: true 60 | DeriveLineEnding: true 61 | DerivePointerAlignment: false 62 | DisableFormat: false 63 | ExperimentalAutoDetectBinPacking: false 64 | FixNamespaceComments: true 65 | ForEachMacros: 66 | - foreach 67 | - Q_FOREACH 68 | - BOOST_FOREACH 69 | IncludeBlocks: Preserve 70 | IncludeCategories: 71 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 72 | Priority: 2 73 | SortPriority: 0 74 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' 75 | Priority: 3 76 | SortPriority: 0 77 | - Regex: '.*' 78 | Priority: 1 79 | SortPriority: 0 80 | IncludeIsMainRegex: '(Test)?$' 81 | IncludeIsMainSourceRegex: '' 82 | IndentCaseLabels: false 83 | IndentGotoLabels: true 84 | IndentPPDirectives: None 85 | IndentWidth: 4 86 | IndentWrappedFunctionNames: false 87 | JavaScriptQuotes: Leave 88 | JavaScriptWrapImports: true 89 | KeepEmptyLinesAtTheStartOfBlocks: true 90 | MacroBlockBegin: '' 91 | MacroBlockEnd: '' 92 | MaxEmptyLinesToKeep: 1 93 | NamespaceIndentation: None 94 | ObjCBinPackProtocolList: Auto 95 | ObjCBlockIndentWidth: 2 96 | ObjCSpaceAfterProperty: false 97 | ObjCSpaceBeforeProtocolList: true 98 | PenaltyBreakAssignment: 2 99 | PenaltyBreakBeforeFirstCallParameter: 19 100 | PenaltyBreakComment: 300 101 | PenaltyBreakFirstLessLess: 120 102 | PenaltyBreakString: 1000 103 | PenaltyBreakTemplateDeclaration: 10 104 | PenaltyExcessCharacter: 1000000 105 | PenaltyReturnTypeOnItsOwnLine: 60 106 | PointerAlignment: Right 107 | ReflowComments: true 108 | SortIncludes: true 109 | SortUsingDeclarations: true 110 | SpaceAfterCStyleCast: false 111 | SpaceAfterLogicalNot: false 112 | SpaceAfterTemplateKeyword: true 113 | SpaceBeforeAssignmentOperators: true 114 | SpaceBeforeCpp11BracedList: false 115 | SpaceBeforeCtorInitializerColon: true 116 | SpaceBeforeInheritanceColon: true 117 | SpaceBeforeParens: ControlStatements 118 | SpaceBeforeRangeBasedForLoopColon: true 119 | SpaceInEmptyBlock: false 120 | SpaceInEmptyParentheses: false 121 | SpacesBeforeTrailingComments: 1 122 | SpacesInAngles: false 123 | SpacesInConditionalStatement: false 124 | SpacesInContainerLiterals: true 125 | SpacesInCStyleCastParentheses: false 126 | SpacesInParentheses: false 127 | SpacesInSquareBrackets: false 128 | SpaceBeforeSquareBrackets: false 129 | Standard: Latest 130 | StatementMacros: 131 | - Q_UNUSED 132 | - QT_REQUIRE_VERSION 133 | TabWidth: 8 134 | UseCRLF: false 135 | UseTab: Never 136 | ... 137 | 138 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | *~ 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | OUT=out 22 | 23 | ############ All ############ 24 | 25 | all: pi host 26 | 27 | ############ Format ############# 28 | 29 | .PHONY: fmt-md 30 | fmt-md: 31 | @find *.md | xargs markdownfmt -w 32 | 33 | .PHONY: fmt-clang 34 | fmt-clang: 35 | @find * -type f -name '*.[c,h]' | grep -v out | xargs clang-format -i 36 | 37 | .PHONY: fmt-go 38 | fmt-go: 39 | @find -type f -name go.mod | xargs dirname | xargs -I X bash -c "cd X && go fmt ./..." 40 | 41 | .PHONY: fmt 42 | fmt: fmt-md fmt-clang fmt-go 43 | 44 | ############ Raspberry Pi ########## 45 | PI_TOOLCHAIN=$(shell pwd)/src/cmake/toolchains/pi.toolchain 46 | PI_OUT=$(OUT)/pi 47 | 48 | .PHONY: pi 49 | pi: $(PI_OUT)/pi.toolchain 50 | @ninja -C $(PI_OUT) 51 | 52 | $(PI_OUT)/pi.toolchain: Makefile $(PI_TOOLCHAIN) 53 | rm -rf $(PI_OUT) && \ 54 | mkdir -p $(PI_OUT) && \ 55 | cmake -DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_TOOLCHAIN_FILE=$(PI_TOOLCHAIN) -B$(PI_OUT) src 56 | cp $(PI_TOOLCHAIN) $(PI_OUT)/pi.toolchain 57 | 58 | ########### Host ############# 59 | HOST_TOOLCHAIN=$(shell pwd)/src/cmake/toolchains/host.toolchain 60 | HOST_OUT=$(OUT)/host 61 | 62 | .PHONY: host 63 | host: $(HOST_OUT)/host.toolchain 64 | @ninja -C $(HOST_OUT) 65 | 66 | $(HOST_OUT)/host.toolchain: Makefile $(HOST_TOOLCHAIN) 67 | rm -rf $(HOST_OUT) && \ 68 | mkdir -p $(HOST_OUT) && \ 69 | cmake -DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_TOOLCHAIN_FILE=$(HOST_TOOLCHAIN) -B$(HOST_OUT) src 70 | cp $(HOST_TOOLCHAIN) $(HOST_OUT)/host.toolchain 71 | 72 | ######### Clean ########### 73 | 74 | .PHONY: clean 75 | clean: 76 | rm -rf $(OUT) 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cmake_go: CMake support for mixing Go and C/C++ 2 | =============================================== 3 | 4 | Introduction 5 | ------------ 6 | 7 | This project aims to make it easy for projects using CMake to incorporate Go (Golang) into their existing applications. The CMake macro "add\_cgo\_executable" takes¨ a list of .go files and CMake (library) targets and creates a new CMake target for a set of Go applications. The user does not need to specify any flags to CGO manually, it is all taken care of by the macro. 8 | 9 | Cross compilation 10 | ----------------- 11 | 12 | Support for building for the host platform and Raspberry PI (ARM) is provided. 13 | 14 | Static code analysis 15 | -------------------- 16 | 17 | go.cmake executes a number of linters from golangci-lint during buildtime. 18 | 19 | ASAN 20 | ---- 21 | 22 | In addition to binding together Go and C/C++ this project also incorporates ASAN support. 23 | 24 | Examples 25 | -------- 26 | 27 | The "examples" directory contains several examples 28 | 29 | - goodprogram 30 | 31 | A small Go program that invokes some C-code. 32 | 33 | - leakyprogram 34 | 35 | A small Go program that invokes some C-code and leaks memory. This is detected by ASAN. 36 | 37 | - nullrefprogram 38 | 39 | A small Go program that invokes some C-code and reads from a NULL pointer. This is detected by ASAN. 40 | 41 | - outofboundsprogram 42 | 43 | A small Go program that invokes some C-code and writes out of bounds. This is detected by ASAN. 44 | 45 | Building and running (Ubuntu/Debian) 46 | ------------------------------------ 47 | 48 | Install a recent version of Go (https://tip.golang.org/dl/\) 49 | 50 | $ apt install clang-11 build-essential gcc-8-arm-linux-gnueabihf 51 | 52 | $ go get -u github.com/shurcooL/markdownfmt 53 | 54 | Build 55 | 56 | $ make 57 | 58 | Run the three examples for host 59 | 60 | $ ./out/host/examples/goodprogram/goodprogram 61 | 62 | $ ./out/host/examples/leakyprogram/leakyprogram 63 | 64 | $ ./out/host/examples/segfaultyprogram/nullrefprogram 65 | 66 | $ ./out/host/examples/segfaultyprogram/outofboundsprogram 67 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | cmake_minimum_required(VERSION 3.14) 22 | 23 | project(cmake_cgo) 24 | 25 | set(ASAN_ENABLED ON) 26 | 27 | include(cmake/go.cmake) 28 | include(cmake/asan.cmake) 29 | 30 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") 31 | 32 | add_subdirectory(libs) 33 | add_subdirectory(examples) 34 | 35 | -------------------------------------------------------------------------------- /src/cmake/asan.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | if (ASAN_ENABLED) 22 | list(APPEND CMAKE_EXE_LINKER_FLAGS "-fsanitize=address") 23 | list(APPEND CMAKE_C_FLAGS "-fsanitize=address") 24 | endif() 25 | -------------------------------------------------------------------------------- /src/cmake/go.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(CGO_RESOLVE_BLACKLIST 22 | pthread 23 | rt 24 | gcov 25 | systemd 26 | ) 27 | 28 | set(CGO_CFLAGS_BLACKLIST 29 | "-Werror" 30 | "-Wall" 31 | "-Wextra" 32 | "-Wold-style-definition" 33 | "-fdiagnostics-color=always" 34 | "-Wformat-nonliteral" 35 | "-Wformat=2" 36 | ) 37 | 38 | macro(add_cgo_executable GO_MOD_NAME GO_FILES CGO_DEPS GO_BIN) 39 | cgo_fetch_cflags_and_ldflags(${CGO_DEPS}) 40 | cgo_build_envs(${GO_BIN}) 41 | 42 | set(CGO_BUILT_FLAG ${CMAKE_CURRENT_BINARY_DIR}/${GO_MOD_NAME}.cgo.module) 43 | add_custom_command( 44 | OUTPUT ${CGO_BUILT_FLAG} 45 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 46 | COMMAND echo Building CGO modules for ${GO_BIN} 47 | COMMAND ${CMAKE_COMMAND} -E remove ${CGO_BUILT_FLAG} 48 | COMMAND env ${CGO_ENVS} go build -a -o ${GO_BIN} ./... 49 | COMMAND touch ${CGO_BUILT_FLAG} 50 | DEPENDS ${CGO_DEPS_HANDLED} 51 | ) 52 | add_custom_target(${GO_MOD_NAME}_cgo ALL 53 | DEPENDS ${CGO_BUILT_FLAG} 54 | ) 55 | 56 | set(GO_BUILT_FLAG ${CMAKE_CURRENT_BINARY_DIR}/${GO_MOD_NAME}.go.module) 57 | add_custom_command( 58 | OUTPUT ${GO_BUILT_FLAG} 59 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 60 | COMMAND echo Building GO modules for ${GO_BIN} 61 | COMMAND env ${CGO_ENVS} go build -o ${GO_BIN} ./... 62 | COMMAND env ${CGO_ENVS} golangci-lint run --enable-all --disable=exhaustivestruct,godot,goerr113,gomnd,nlreturn,wrapcheck,wsl 63 | COMMAND touch ${GO_BUILT_FLAG} 64 | DEPENDS ${CGO_BUILT_FLAG} ${${GO_FILES}} 65 | ) 66 | add_custom_target(${GO_MOD_NAME}_go ALL 67 | DEPENDS ${GO_BUILT_FLAG} 68 | ) 69 | 70 | endmacro() 71 | 72 | 73 | macro(cgo_build_envs GO_BIN) 74 | set(CGO_ENVS 75 | CGO_ENABLED=1 76 | CC=${CMAKE_C_COMPILER} 77 | CGO_CFLAGS="${CGO_CFLAGS}" 78 | CGO_LDFLAGS="${CGO_LDFLAGS}" 79 | ) 80 | 81 | # Assume ARM7 if on ARM 82 | if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm") 83 | list(APPEND CGO_ENVS GOARCH=arm GOARM=7) 84 | endif() 85 | endmacro() 86 | 87 | 88 | macro(cgo_fetch_cflags_and_ldflags CGO_DEPS) 89 | set(CGO_LDFLAGS "") 90 | set(CGO_CFLAGS "") 91 | 92 | set(CGO_DEPS_STACK ${${CGO_DEPS}}) 93 | set(CGO_DEPS_HANDLED "") 94 | set(CGO_RPATHS "") 95 | 96 | while (CGO_DEPS_STACK) 97 | foreach(L ${CGO_DEPS_STACK}) 98 | # Skip if already handled once 99 | if ("${L}" IN_LIST CGO_DEPS_HANDLED) 100 | continue() 101 | endif() 102 | 103 | # Don't resolve system libs 104 | if ("${L}" IN_LIST CGO_RESOLVE_BLACKLIST) 105 | continue() 106 | endif() 107 | 108 | # Resolve PkgConfig libs 109 | if ("${L}" MATCHES "PkgConfig") 110 | get_target_property(L_INTERFACE_LIB ${L} INTERFACE_LINK_LIBRARIES) 111 | 112 | # Mark handled 113 | list(APPEND CGO_DEPS_HANDLED ${L}) 114 | 115 | # Finally override L 116 | set(L "${L_INTERFACE_LIB}") 117 | 118 | # L might be a list so iterate over it when adding rpaths 119 | foreach (L_ITEM ${L}) 120 | # Fetch directory and add it to rpath-link if not already added 121 | get_filename_component(R "${L_ITEM}" DIRECTORY) 122 | 123 | if (NOT "${R}" IN_LIST CGO_RPATHS) 124 | # Linker may need to find private libraries in the same directory 125 | list(APPEND CGO_RPATHS "${R}") 126 | endif() 127 | endforeach() 128 | 129 | # Add libraries to linker flags 130 | list(APPEND CGO_LDFLAGS ${L}) 131 | else() 132 | # Try resolve alias 133 | get_target_property(L_ALIASED ${L} ALIASED_TARGET) 134 | if (NOT "${L_ALIASED}" MATCHES "NOTFOUND") 135 | set(L "${L_ALIASED}") 136 | endif() 137 | 138 | # Mark handled 139 | list(APPEND CGO_DEPS_HANDLED ${L}) 140 | 141 | get_target_property(L_INCLUDES ${L} INCLUDE_DIRECTORIES) 142 | get_target_property(L_BUILD_DIR ${L} BINARY_DIR) 143 | 144 | list(APPEND CGO_LDFLAGS -L${L_BUILD_DIR}) 145 | list(APPEND CGO_LDFLAGS -l${L}) 146 | 147 | foreach(I ${L_INCLUDES}) 148 | list(APPEND CGO_CFLAGS -I${I}) 149 | endforeach() 150 | 151 | list(REMOVE_ITEM CGO_DEPS_STACK "${L}") 152 | 153 | get_target_property(DEPS ${L} LINK_LIBRARIES) 154 | foreach(D ${DEPS}) 155 | list(APPEND CGO_DEPS_STACK "${D}") 156 | endforeach() 157 | 158 | endif() 159 | 160 | endforeach() 161 | endwhile() 162 | 163 | #### Adding cflags and ldflags ##### 164 | 165 | # Must split sentences into CMake List before adding cflags and ldflags 166 | string(REPLACE " " ";" CMAKE_C_FLAGS_LIST ${CMAKE_C_FLAGS}) 167 | list(APPEND CGO_CFLAGS ${CMAKE_C_FLAGS_LIST}) 168 | 169 | string(REPLACE " " ";" CMAKE_EXE_LINKER_FLAGS_LIST "${CMAKE_EXE_LINKER_FLAGS}") 170 | list(APPEND CGO_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS_LIST}") 171 | string(REPLACE " " ";" CMAKE_C_LINK_FLAGS_LIST "${CMAKE_C_LINK_FLAGS}") 172 | list(APPEND CGO_LDFLAGS "${CMAKE_C_LINK_FLAGS_LIST}") 173 | 174 | if(CMAKE_BUILD_TYPE MATCHES debug) 175 | string(REPLACE " " ";" CMAKE_C_FLAGS_DEBUG_LIST "${CMAKE_C_FLAGS_DEBUG}") 176 | list(APPEND CGO_CFLAGS ${CMAKE_C_FLAGS_DEBUG_LIST}) 177 | string(REPLACE " " ";" CMAKE_EXE_LINKER_FLAGS_DEBUG_LIST "${CMAKE_EXE_LINKER_FLAGS_DEBUG}") 178 | list(APPEND CGO_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS_DEBUG_LIST}") 179 | endif() 180 | 181 | if(CMAKE_BUILD_TYPE MATCHES release) 182 | string(REPLACE " " ";" CMAKE_C_FLAGS_RELEASE_LIST "${CMAKE_C_FLAGS_RELEASE}") 183 | list(APPEND CGO_CFLAGS ${CMAKE_C_FLAGS_RELEASE_LIST}) 184 | string(REPLACE " " ";" CMAKE_EXE_LINKER_FLAGS_RELEASE_LIST "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") 185 | list(APPEND CGO_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS_RELEASE_LIST}") 186 | endif() 187 | 188 | # Need to remove warnings for CGo to work 189 | foreach(F ${CGO_CFLAGS_BLACKLIST}) 190 | list(REMOVE_ITEM CGO_CFLAGS "${F}") 191 | endforeach() 192 | 193 | # Add rpaths if present 194 | foreach(R ${CGO_RPATHS}) 195 | list(APPEND CGO_LDFLAGS "-Wl,-rpath-link=${R}") 196 | endforeach() 197 | 198 | endmacro() 199 | -------------------------------------------------------------------------------- /src/cmake/toolchains/host.toolchain: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(CMAKE_C_COMPILER clang-11) -------------------------------------------------------------------------------- /src/cmake/toolchains/pi.toolchain: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(CMAKE_SYSTEM_NAME Linux) 22 | set(CMAKE_SYSTEM_PROCESSOR arm) 23 | 24 | set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc-8) -------------------------------------------------------------------------------- /src/examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | add_subdirectory(goodprogram) 22 | add_subdirectory(leakyprogram) 23 | add_subdirectory(segfaultyprogram) 24 | -------------------------------------------------------------------------------- /src/examples/goodprogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(go_files 22 | go/goodprogram/main.go 23 | ) 24 | 25 | set(cgo_deps 26 | stuff 27 | ) 28 | 29 | add_cgo_executable(goodprogram go_files cgo_deps ${CMAKE_CURRENT_BINARY_DIR}) 30 | -------------------------------------------------------------------------------- /src/examples/goodprogram/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/krumberg/cmake_go 2 | 3 | go 1.15 4 | -------------------------------------------------------------------------------- /src/examples/goodprogram/go/goodprogram/main.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | package main 22 | 23 | import ( 24 | "fmt" 25 | ) 26 | 27 | // #include "stuff.h" 28 | // 29 | // void __lsan_do_leak_check(void); 30 | // 31 | import "C" 32 | 33 | func main() { 34 | defer C.__lsan_do_leak_check() 35 | 36 | s := C.stuff_create(1234) 37 | defer C.stuff_destroy(s) 38 | 39 | fmt.Println("Value is", C.stuff_get_value(s)) 40 | } 41 | -------------------------------------------------------------------------------- /src/examples/leakyprogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(go_files 22 | go/leakyprogram/main.go 23 | ) 24 | 25 | set(cgo_deps 26 | stuff 27 | ) 28 | 29 | add_cgo_executable(leakyprogram go_files cgo_deps ${CMAKE_CURRENT_BINARY_DIR}) 30 | -------------------------------------------------------------------------------- /src/examples/leakyprogram/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/krumberg/cmake_go 2 | 3 | go 1.15 4 | -------------------------------------------------------------------------------- /src/examples/leakyprogram/go/leakyprogram/main.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | package main 22 | 23 | import ( 24 | "fmt" 25 | ) 26 | 27 | // #include "stuff.h" 28 | // 29 | // void __lsan_do_leak_check(void); 30 | // 31 | import "C" 32 | 33 | func main() { 34 | defer C.__lsan_do_leak_check() 35 | 36 | s := C.stuff_create(1234) 37 | 38 | fmt.Println("Value is", C.stuff_get_value(s)) 39 | } 40 | -------------------------------------------------------------------------------- /src/examples/segfaultyprogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | set(go_files 22 | go/nullrefprogram/main.go 23 | go/outofboundsprogram/main.go 24 | ) 25 | 26 | set(cgo_deps 27 | stuff 28 | ) 29 | 30 | add_cgo_executable(segfaultyprogram go_files cgo_deps ${CMAKE_CURRENT_BINARY_DIR}) 31 | -------------------------------------------------------------------------------- /src/examples/segfaultyprogram/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/krumberg/cmake_go 2 | 3 | go 1.15 4 | -------------------------------------------------------------------------------- /src/examples/segfaultyprogram/go/nullrefprogram/main.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | package main 22 | 23 | import ( 24 | "fmt" 25 | ) 26 | 27 | // #include "stuff.h" 28 | // 29 | // void __lsan_do_leak_check(void); 30 | // 31 | import "C" 32 | 33 | func main() { 34 | defer C.__lsan_do_leak_check() 35 | 36 | s := C.stuff_create(0) 37 | defer C.stuff_destroy(s) 38 | 39 | fmt.Println("Value is", C.stuff_get_value(s)) 40 | } 41 | -------------------------------------------------------------------------------- /src/examples/segfaultyprogram/go/outofboundsprogram/main.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | package main 22 | 23 | // #include "myarray.h" 24 | // 25 | // void __lsan_do_leak_check(void); 26 | import "C" 27 | 28 | func main() { 29 | defer C.__lsan_do_leak_check() 30 | 31 | a := C.myarray_create(1000) 32 | defer C.myarray_destroy(a) 33 | 34 | C.myarray_set(a, 1000, 1) 35 | } 36 | -------------------------------------------------------------------------------- /src/libs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | add_subdirectory(common) 22 | add_subdirectory(stuff) 23 | -------------------------------------------------------------------------------- /src/libs/common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | add_library(common 22 | src/mylog.c 23 | src/myarray.c 24 | ) 25 | 26 | target_include_directories(common PUBLIC include) 27 | target_link_libraries(common) 28 | -------------------------------------------------------------------------------- /src/libs/common/include/myarray.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #pragma once 22 | 23 | typedef struct MyArray_ MyArray; 24 | 25 | MyArray *myarray_create(int size); 26 | 27 | void myarray_set(MyArray *self, int index, int value); 28 | 29 | void myarray_destroy(MyArray *self); 30 | -------------------------------------------------------------------------------- /src/libs/common/include/mylog.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #pragma once 22 | 23 | void mylog(const char *msg); 24 | -------------------------------------------------------------------------------- /src/libs/common/src/myarray.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #include "myarray.h" 22 | 23 | #include 24 | 25 | struct MyArray_ 26 | { 27 | int buff[0]; 28 | }; 29 | 30 | MyArray *myarray_create(int size) 31 | { 32 | MyArray *self = malloc(sizeof(*self) + size * sizeof(int)); 33 | return self; 34 | } 35 | 36 | void myarray_set(MyArray *self, int index, int value) 37 | { 38 | self->buff[index] = value; 39 | } 40 | 41 | void myarray_destroy(MyArray *self) 42 | { 43 | free(self); 44 | } 45 | -------------------------------------------------------------------------------- /src/libs/common/src/mylog.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #include "mylog.h" 22 | 23 | #include 24 | 25 | void mylog(const char *msg) 26 | { 27 | printf("%s\n", msg); 28 | } 29 | -------------------------------------------------------------------------------- /src/libs/stuff/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | 21 | add_library(stuff 22 | src/stuff.c 23 | ) 24 | 25 | target_include_directories(stuff PUBLIC include) 26 | target_link_libraries(stuff common) 27 | -------------------------------------------------------------------------------- /src/libs/stuff/include/stuff.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #pragma once 22 | 23 | typedef struct Stuff_ Stuff; 24 | 25 | Stuff *stuff_create(int value); 26 | 27 | int stuff_get_value(Stuff *self); 28 | 29 | void stuff_destroy(Stuff *self); 30 | -------------------------------------------------------------------------------- /src/libs/stuff/src/stuff.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 Kristian Rumberg (kristianrumberg@gmail.com) 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | #include "stuff.h" 22 | 23 | #include "mylog.h" 24 | 25 | #include 26 | 27 | struct Stuff_ 28 | { 29 | int value; 30 | }; 31 | 32 | Stuff *stuff_create(int value) 33 | { 34 | if (value == 0) 35 | { 36 | return NULL; 37 | } 38 | 39 | mylog("Creating stuff"); 40 | 41 | Stuff *s = malloc(sizeof(*s)); 42 | s->value = value; 43 | 44 | return s; 45 | } 46 | 47 | int stuff_get_value(Stuff *self) 48 | { 49 | return self->value; 50 | } 51 | 52 | void stuff_destroy(Stuff *self) 53 | { 54 | if (self) 55 | { 56 | mylog("Destroy stuff"); 57 | 58 | free(self); 59 | } 60 | } 61 | --------------------------------------------------------------------------------