From b3ec14bf768267a5d5d3c0da3ebd231e69ac7206 Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Sat, 9 Nov 2019 20:01:00 +0100 Subject: [PATCH] add SharedLibrary example --- CMakeLists.txt | 2 + Samples/SharedLibrary/CMakeLists.txt | 68 +++++++++++++++++++++++++++ Samples/SharedLibrary/application.c | 35 ++++++++++++++ Samples/SharedLibrary/libLibrary.a | Bin 0 -> 5056 bytes Samples/SharedLibrary/library.c | 45 ++++++++++++++++++ Samples/SharedLibrary/library.exp | 2 + Samples/SharedLibrary/library.h | 23 +++++++++ Samples/SharedLibrary/library.o | Bin 0 -> 4640 bytes Samples/SharedLibrary/library.r | 38 +++++++++++++++ 9 files changed, 213 insertions(+) create mode 100644 Samples/SharedLibrary/CMakeLists.txt create mode 100644 Samples/SharedLibrary/application.c create mode 100644 Samples/SharedLibrary/libLibrary.a create mode 100644 Samples/SharedLibrary/library.c create mode 100644 Samples/SharedLibrary/library.exp create mode 100644 Samples/SharedLibrary/library.h create mode 100755 Samples/SharedLibrary/library.o create mode 100644 Samples/SharedLibrary/library.r diff --git a/CMakeLists.txt b/CMakeLists.txt index 6835d7c9e2..dbbf80547e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,8 @@ add_subdirectory(Samples/Raytracer) if(CMAKE_SYSTEM_NAME MATCHES Retro68) add_subdirectory(Samples/Launcher) add_subdirectory(Samples/SystemExtension) +else() +add_subdirectory(Samples/SharedLibrary) endif() if(CMAKE_SYSTEM_NAME MATCHES RetroCarbon) diff --git a/Samples/SharedLibrary/CMakeLists.txt b/Samples/SharedLibrary/CMakeLists.txt new file mode 100644 index 0000000000..6cff3fff9c --- /dev/null +++ b/Samples/SharedLibrary/CMakeLists.txt @@ -0,0 +1,68 @@ +# Copyright 2019 Wolfgang Thaller. +# +# This file is part of Retro68. +# +# Retro68 is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Retro68 is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Retro68. If not, see . + + +# This example does nothing useful, but it does it using a shared library. +# Doesn't run on 68K. + +# Step 1: Tell CMake to compile the library. +# (CMake writes the output to an XCOFF file named libLibrary.so) +add_library(Library SHARED library.c library.h) + +# Step 2: Tell the linker to export symbols. + +# Either export everything: +# target_link_options(Library PUBLIC -Wl,-bexpfull) + +# Or tell it to use an export list from a separate file: +target_link_options(Library PUBLIC -Wl,-bE:${CMAKE_CURRENT_SOURCE_DIR}/library.exp) + +# Note: Step 1 & 2 are equivalent to the command: +# powerpc-apple-macos-gcc -shared -bE:library.exp library.c -o libLibrary.so + +# Step 3: Convert the library to PEF format: + +add_custom_command( + OUTPUT Library.pef + COMMAND ${MAKE_PEF} "libLibrary.so" -o "Library.pef" + DEPENDS Library) + +# Step 4: Combine the PEF data fork with a resource fork containing a cfrg resource: + +add_custom_command( + OUTPUT Library.bin Library Library.dsk Library.ad "%Library.ad" + COMMAND ${REZ} + ${REZ_FLAGS} + ${CMAKE_CURRENT_SOURCE_DIR}/library.r + -I${REZ_INCLUDE_PATH} + -o "Library.bin" --cc "Library.dsk" --cc "Library" + --cc "%Library.ad" + -t "shlb" -c "????" + --data Library.pef + + DEPENDS Library.pef ${rsrc_files}) + +# Step 5: Create a CMake custom target so CMake knows that Steps 3 and 4 need to be done +add_custom_target(Library_APPL ALL DEPENDS Library.bin) + +# Step 6: Create the main application +# (the add_applicatin macro works similarly to the commands above, but for applications.) + +add_application(Application application.c) + +# Step 7: Link the application to the library target (the XCOFF version of the library!) +target_link_libraries(Application Library) diff --git a/Samples/SharedLibrary/application.c b/Samples/SharedLibrary/application.c new file mode 100644 index 0000000000..2c1b3eec8a --- /dev/null +++ b/Samples/SharedLibrary/application.c @@ -0,0 +1,35 @@ +/* + Copyright 2019 Wolfgang Thaller. + + This file is part of Retro68. + + Retro68 is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Retro68 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Retro68. If not, see . +*/ + +/* + Main application program that uses two functions exported by a shared library. + */ + +#include "library.h" + +int main() +{ + // wait until computer is turned on ;-) + while(!is_computer_on()) + ; + + beep(); + + return 0; +} diff --git a/Samples/SharedLibrary/libLibrary.a b/Samples/SharedLibrary/libLibrary.a new file mode 100644 index 0000000000000000000000000000000000000000..997d13f17008deaf1c64429104810bbb827d3804 GIT binary patch literal 5056 zcmeHL(N4lJ6z!1kfG_$7`vKZ*Y=s$OjERXRJ{a^JyHdt<&}Ewe-~3bmL~R{|rPhdv zPo_6Zde3QhdVBiRJBpH+%uc)hF!XFC90g0L<6Lj}Wu=Y-n((pzMRMO-++fh}du}-F z+qUaCTOB1I1VL3^D26nNa*{to4(_|wD`;0G9k0dM0heCKIs{g;%y$tr-sBc&38oDW z#MmBRh*1-;Wb0DHE0hhVq$CQdfoCu(3JtGPFEHg~%JK$piL)IwuuN5~OdDt%Fb)_8 zj0462n#Z71qz`Hos&OTL=f&i-M*rx zU+?;jj^YTQ)K)jF~-Q|WAGHeH. +*/ + +/* + Library source file exporting two functions. + Note that Retro68 does not yet correctly implement exporting variables. + */ + +#include "library.h" +#include + +void beep() +{ + SysBeep(20); +} + +/* + * is_computer_on + * + * For compatibility with BeOS ;-) + * + * Returns true if the computer is currently powered on. + * Note that a function by this name was an actual, documented part + * of the BeOS API. + */ +Boolean is_computer_on() +{ + return true; +} diff --git a/Samples/SharedLibrary/library.exp b/Samples/SharedLibrary/library.exp new file mode 100644 index 0000000000..8f31c9520a --- /dev/null +++ b/Samples/SharedLibrary/library.exp @@ -0,0 +1,2 @@ +beep +is_computer_on diff --git a/Samples/SharedLibrary/library.h b/Samples/SharedLibrary/library.h new file mode 100644 index 0000000000..1bf829c157 --- /dev/null +++ b/Samples/SharedLibrary/library.h @@ -0,0 +1,23 @@ +/* + Copyright 2019 Wolfgang Thaller. + + This file is part of Retro68. + + Retro68 is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Retro68 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Retro68. If not, see . +*/ + +#include + +void beep(); +Boolean is_computer_on(); diff --git a/Samples/SharedLibrary/library.o b/Samples/SharedLibrary/library.o new file mode 100755 index 0000000000000000000000000000000000000000..825e89ea88da2df478578151f33f47879ce47877 GIT binary patch literal 4640 zcmeI0K}*9h6vvZUcJZR$U|(R#Hn+@)2N47X4>G-%G|Reb)>o3qwwvG7Z{keX7E7s! z;7Rfbd3pc9OWyCD68w$=bgh>f&RB3$2*<&Z@XvW7Kgm0=|&CrejyzNiEKo&A74cLr=gH@B};oPrwuK1Uvyx zz!UHUJONMOe-Ws?_3@J{Eq~efsuJl6){<^OEn}X>^g+Y36xPtWv>MoXKYC5nf?5D2 zRy;|mN+KEyK{MD$orylrG9~+KF2K+k8Ir-ppJ$|me2unq-JO1Y>Nk0EEH2$$YNcLs zA@7rjaFndGFsy6gc*f*(%qG(jV{FLI*kml&jE%wpfdg8yOq47j2m23%V(ijtu6INf ys+-B<&Rk2GRYIrMsY. +*/ + +#include "Processes.r" +#include "CodeFragments.r" + +/* + cfrg resource for a library. + It specifies the library name, which must match the name of the library target + in CMakeLists.txt. + Note that the actual file name does *not* need to match, renaming the file does *not* + break things. How cool is that? +*/ + +resource 'cfrg' (0) { + { + kPowerPCCFragArch, kIsCompleteCFrag, kNoVersionNum, kNoVersionNum, + kDefaultStackSize, kNoAppSubFolder, + kImportLibraryCFrag, kDataForkCFragLocator, kZeroOffset, kCFragGoesToEOF, + "Library" + } +};