docs/README-macosx.md
author Sam Lantinga
Tue, 29 Jul 2014 08:04:15 -0700
changeset 9025 d09d4b578e77
parent 9023 doc/README-macosx.md@276802355854
child 9066 c2af3ff967cc
permissions -rw-r--r--
Moved documentation to docs, recreated a simple README.txt, fixed build
     1 Mac OS X
     2 ==============================================================================
     3 
     4 These instructions are for people using Apple's Mac OS X (pronounced
     5 "ten").
     6 
     7 From the developer's point of view, OS X is a sort of hybrid Mac and
     8 Unix system, and you have the option of using either traditional
     9 command line tools or Apple's IDE Xcode.
    10 
    11 To build SDL using the command line, use the standard configure and make
    12 process:
    13 
    14 	./configure
    15 	make
    16 	sudo make install
    17 
    18 You can also build SDL as a Universal library (a single binary for both
    19 32-bit and 64-bit Intel architectures), on Mac OS X 10.7 and newer, by using
    20 the fatbuild.sh script in build-scripts:
    21 	sh build-scripts/fatbuild.sh
    22 	sudo build-scripts/fatbuild.sh install
    23 This script builds SDL with 10.5 ABI compatibility on i386 and 10.6
    24 ABI compatibility on x86_64 architectures.  For best compatibility you
    25 should compile your application the same way.  A script which wraps
    26 gcc to make this easy is provided in test/gcc-fat.sh
    27 
    28 Please note that building SDL requires at least Xcode 4.6 and the 10.7 SDK
    29 (even if you target back to 10.5 systems). PowerPC support for Mac OS X has
    30 been officially dropped as of SDL 2.0.2.
    31 
    32 To use the library once it's built, you essential have two possibilities:
    33 use the traditional autoconf/automake/make method, or use Xcode.
    34 
    35 ==============================================================================
    36 Caveats for using SDL with Mac OS X
    37 ==============================================================================
    38 
    39 Some things you have to be aware of when using SDL on Mac OS X:
    40 
    41 - If you register your own NSApplicationDelegate (using [NSApp setDelegate:]),
    42   SDL will not register its own. This means that SDL will not terminate using
    43   SDL_Quit if it receives a termination request, it will terminate like a 
    44   normal app, and it will not send a SDL_DROPFILE when you request to open a
    45   file with the app. To solve these issues, put the following code in your 
    46   NSApplicationDelegate implementation:
    47 
    48   - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    49   {
    50       if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
    51           SDL_Event event;
    52           event.type = SDL_QUIT;
    53           SDL_PushEvent(&event);
    54       }
    55 
    56       return NSTerminateCancel;
    57   }
    58 
    59   - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
    60   {
    61       if (SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) {
    62           SDL_Event event;
    63           event.type = SDL_DROPFILE;
    64           event.drop.file = SDL_strdup([filename UTF8String]);
    65           return (SDL_PushEvent(&event) > 0);
    66       }
    67 
    68       return NO;
    69   }
    70 
    71 ==============================================================================
    72 Using the Simple DirectMedia Layer with a traditional Makefile
    73 ==============================================================================
    74 
    75 An existing autoconf/automake build system for your SDL app has good chances
    76 to work almost unchanged on OS X. However, to produce a "real" Mac OS X binary
    77 that you can distribute to users, you need to put the generated binary into a
    78 so called "bundle", which basically is a fancy folder with a name like
    79 "MyCoolGame.app".
    80 
    81 To get this build automatically, add something like the following rule to
    82 your Makefile.am:
    83 
    84 bundle_contents = APP_NAME.app/Contents
    85 APP_NAME_bundle: EXE_NAME
    86 	mkdir -p $(bundle_contents)/MacOS
    87 	mkdir -p $(bundle_contents)/Resources
    88 	echo "APPL????" > $(bundle_contents)/PkgInfo
    89 	$(INSTALL_PROGRAM) $< $(bundle_contents)/MacOS/
    90 
    91 You should replace EXE_NAME with the name of the executable. APP_NAME is what
    92 will be visible to the user in the Finder. Usually it will be the same
    93 as EXE_NAME but capitalized. E.g. if EXE_NAME is "testgame" then APP_NAME 
    94 usually is "TestGame". You might also want to use @PACKAGE@ to use the package
    95 name as specified in your configure.in file.
    96 
    97 If your project builds more than one application, you will have to do a bit
    98 more. For each of your target applications, you need a separate rule.
    99 
   100 If you want the created bundles to be installed, you may want to add this
   101 rule to your Makefile.am:
   102 
   103 install-exec-hook: APP_NAME_bundle
   104 	rm -rf $(DESTDIR)$(prefix)/Applications/APP_NAME.app
   105 	mkdir -p $(DESTDIR)$(prefix)/Applications/
   106 	cp -r $< /$(DESTDIR)$(prefix)Applications/
   107 
   108 This rule takes the Bundle created by the rule from step 3 and installs them
   109 into $(DESTDIR)$(prefix)/Applications/.
   110 
   111 Again, if you want to install multiple applications, you will have to augment
   112 the make rule accordingly.
   113 
   114 
   115 But beware! That is only part of the story! With the above, you end up with
   116 a bare bone .app bundle, which is double clickable from the Finder. But
   117 there are some more things you should do before shipping your product...
   118 
   119 1) The bundle right now probably is dynamically linked against SDL. That 
   120    means that when you copy it to another computer, *it will not run*,
   121    unless you also install SDL on that other computer. A good solution
   122    for this dilemma is to static link against SDL. On OS X, you can
   123    achieve that by linking against the libraries listed by
   124      sdl-config --static-libs
   125    instead of those listed by
   126      sdl-config --libs
   127    Depending on how exactly SDL is integrated into your build systems, the
   128    way to achieve that varies, so I won't describe it here in detail
   129 2) Add an 'Info.plist' to your application. That is a special XML file which
   130    contains some meta-information about your application (like some copyright
   131    information, the version of your app, the name of an optional icon file,
   132    and other things). Part of that information is displayed by the Finder
   133    when you click on the .app, or if you look at the "Get Info" window.
   134    More information about Info.plist files can be found on Apple's homepage.
   135 
   136 
   137 As a final remark, let me add that I use some of the techniques (and some
   138 variations of them) in Exult and ScummVM; both are available in source on
   139 the net, so feel free to take a peek at them for inspiration!
   140 
   141 
   142 ==============================================================================
   143 Using the Simple DirectMedia Layer with Xcode
   144 ==============================================================================
   145 
   146 These instructions are for using Apple's Xcode IDE to build SDL applications.
   147 
   148 - First steps
   149 
   150 The first thing to do is to unpack the Xcode.tar.gz archive in the
   151 top level SDL directory (where the Xcode.tar.gz archive resides).
   152 Because Stuffit Expander will unpack the archive into a subdirectory,
   153 you should unpack the archive manually from the command line:
   154 	cd [path_to_SDL_source]
   155 	tar zxf Xcode.tar.gz
   156 This will create a new folder called Xcode, which you can browse
   157 normally from the Finder.
   158 
   159 - Building the Framework
   160 
   161 The SDL Library is packaged as a framework bundle, an organized
   162 relocatable folder hierarchy of executable code, interface headers,
   163 and additional resources. For practical purposes, you can think of a 
   164 framework as a more user and system-friendly shared library, whose library
   165 file behaves more or less like a standard UNIX shared library.
   166 
   167 To build the framework, simply open the framework project and build it. 
   168 By default, the framework bundle "SDL.framework" is installed in 
   169 /Library/Frameworks. Therefore, the testers and project stationary expect
   170 it to be located there. However, it will function the same in any of the
   171 following locations:
   172 
   173     ~/Library/Frameworks
   174     /Local/Library/Frameworks
   175     /System/Library/Frameworks
   176 
   177 - Build Options
   178     There are two "Build Styles" (See the "Targets" tab) for SDL.
   179     "Deployment" should be used if you aren't tweaking the SDL library.
   180     "Development" should be used to debug SDL apps or the library itself.
   181 
   182 - Building the Testers
   183     Open the SDLTest project and build away!
   184 
   185 - Using the Project Stationary
   186     Copy the stationary to the indicated folders to access it from
   187     the "New Project" and "Add target" menus. What could be easier?
   188 
   189 - Setting up a new project by hand
   190     Some of you won't want to use the Stationary so I'll give some tips:
   191     * Create a new "Cocoa Application"
   192     * Add src/main/macosx/SDLMain.m , .h and .nib to your project
   193     * Remove "main.c" from your project
   194     * Remove "MainMenu.nib" from your project
   195     * Add "$(HOME)/Library/Frameworks/SDL.framework/Headers" to include path
   196     * Add "$(HOME)/Library/Frameworks" to the frameworks search path
   197     * Add "-framework SDL -framework Foundation -framework AppKit" to "OTHER_LDFLAGS"
   198     * Set the "Main Nib File" under "Application Settings" to "SDLMain.nib"
   199     * Add your files
   200     * Clean and build
   201 
   202 - Building from command line
   203     Use pbxbuild in the same directory as your .pbproj file
   204 
   205 - Running your app
   206     You can send command line args to your app by either invoking it from
   207     the command line (in *.app/Contents/MacOS) or by entering them in the
   208     "Executables" panel of the target settings.
   209     
   210 - Implementation Notes
   211     Some things that may be of interest about how it all works...
   212     * Working directory
   213         As defined in the SDL_main.m file, the working directory of your SDL app
   214         is by default set to its parent. You may wish to change this to better
   215         suit your needs.
   216     * You have a Cocoa App!
   217         Your SDL app is essentially a Cocoa application. When your app
   218         starts up and the libraries finish loading, a Cocoa procedure is called,
   219         which sets up the working directory and calls your main() method.
   220         You are free to modify your Cocoa app with generally no consequence 
   221         to SDL. You cannot, however, easily change the SDL window itself.
   222         Functionality may be added in the future to help this.
   223 
   224 
   225 Known bugs are listed in the file "BUGS"