add SharedLibrary example

This commit is contained in:
Wolfgang Thaller 2019-11-09 20:01:00 +01:00
parent 7309f84f1a
commit b3ec14bf76
9 changed files with 213 additions and 0 deletions

View File

@ -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)

View File

@ -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 <http://www.gnu.org/licenses/>.
# 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)

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/*
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;
}

Binary file not shown.

View File

@ -0,0 +1,45 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
/*
Library source file exporting two functions.
Note that Retro68 does not yet correctly implement exporting variables.
*/
#include "library.h"
#include <Sound.h>
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;
}

View File

@ -0,0 +1,2 @@
beep
is_computer_on

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <MacTypes.h>
void beep();
Boolean is_computer_on();

BIN
Samples/SharedLibrary/library.o Executable file

Binary file not shown.

View File

@ -0,0 +1,38 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#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"
}
};