Section 2.13. Programming


2.13. Programming

Mac OS X includes an integrated development environment (IDE) called Xcode, numerous general-purpose and special-purpose libraries, compilers and interpreters for various programming languages, and a rich set of debugging and optimization tools.

2.13.1. Xcode

The development environment provided by Xcode has the following noteworthy features:

  • Support for creating Universal Binaries

  • A file browser and organizer

  • Support for project window configurations called workspaces,[67] which let you choose a preferred layout for Xcode's on-screen components

    [67] Xcode comes with multiple preconfigured project workspaces, such as Default, Condensed, and All-In-One.

  • A source code editor with code completion, syntax highlighting, symbol indexing, and embedded editors in most windows, allowing source viewing and modification without switching windows

  • A class browser

  • Background indexing of project files to improve the performance of features such as class browsing and code completion

  • A documentation viewer that can link symbols in the code to documentation and otherwise allows viewing and searching Apple documentation

  • The Interface Builder application, which provides a graphical user interface for laying out interface objects, customizing them (resizing, setting, and modifying attributes), connecting objects, and so on

  • A built-in visual design tool that lets you create persistence models (for use with the Core Data framework) and class models (for C++, Java, and Objective-C classes)

  • Distributed buildsfor example, across several machines on a networkvia integration with the distcc open source distributed front-end to the GNU C compiler

  • GDB-based graphical and command-line debugging, including remote graphical debugging[68]

    [68] Remote debugging uses SSH public-key authentication while connecting to the remote computer. Xcode can use the ssh-agent helper application for this purpose.

  • Predictive compilation, which runs the compiler in the background as you edit a single source file, with the expectation that once you are ready to build, most of the building may already have been done

  • Precompiled headers, a feature that improves compilation speed (Figure 228 shows an example)

    Figure 228. Using precompiled headers

    $ cat foo.h #define FOO 10 $ cat foo.c #include "foo.h" #include <stdio.h> int main(void) {     printf("%d\n", FOO);     return 0; } $ gcc -x c-header -c foo.h $ ls foo* foo.c           foo.h           foo.h.gch $ file foo.h.gch foo.h.gch: GCC precompiled header (version 012) for C $ rm foo.h $ gcc -o foo foo.c $ ./foo 10

  • ZeroLink, a feature that causes linking to occur at runtime instead of compile time, whereby only code needed to run the application is linked and loaded

    When ZeroLink is used, Xcode generates an application stub containing the full paths to the relevant object files, which are linked, as needed, at runtime. Note that ZeroLink is intended for use only during developmentit requires you to run the application from within Xcode. In other words, applications compiled with ZeroLink enabled cannot be deployed.


  • Fix and Continue, a feature that allows you to make a minor change to your code, have the code compiled, and have it inserted into a running program through in-memory patching[69]

    [69] There are several restrictions on the types of changes accommodated under Fix and Continue.

  • Dead-Code Stripping, a feature that makes the static linker strip unused code and data from executables, thereby potentially reducing their sizes and memory footprints

  • Support for browsing memory and global variables in the debugger

  • Support for launching software performance analysis tools

  • Support for automating the build process using AppleScript

  • Support for multiple version control systems such as CVS, Perforce, and Subversion

A new Xcode project can be instantiated from a large number of templates, depending on the type of application, programming language, target environment, and so on. Supported languages include AppleScript, C, C++, Java, Objective-C, and Objective-C++. Examples of supported templates include those for Automator Actions, Image Unit Plug-ins, Metadata Importers, Preference Panes, Screen Savers, and Sherlock Channels.

Although Xcode is normally used through its graphical user interface, you can also work with existing Xcode projects from the command line. The xcodebuild command-line program can be used to build one or more targets contained in an Xcode project, optionally with a specific build style such as Development or Deployment. The pbprojectdump command-line program can be used to dump an Xcode project dictionary in a human-readable format, thus allowing you to view the project structure. If you must avoid Xcode altogether, you can manage your projects "manually"for example, by creating makefiles and tracking dependencies. Mac OS X includes the BSD and GNU versions of the make program: bsdmake and gnumake, respectively.

The back-end of the Xcode build system is based on the Jam product (/Developer/Private/jam) from Perforce Software, Inc.


2.13.2. Compilers and Libraries

Apple provides a customized and optimized version of the GNU C compiler with back-ends for multiple languages. As we saw earlier in this chapter, two Java compilers are included. Other compilers for a variety of languages are available both commercially[70] and freely.[71] The situation is similar for libraries: Several are included with Mac OS X and several can be compiled from source. In particular, Mac OS X includes some optimized, special-purpose libraries, for example, BLAS, LAPACK, vBigNum, vDSP, vImage, and vMathLib. All these libraries, which are meant for image processing or numerical and scientific computing, are accessible through the Accelerate umbrella framework (Accelerate.framework).

[70] Commercially available compilers include those from Intel and Absoft Corporation.

[71] Numerous open source compilers, interpreters, and libraries can be readily compiled from source on Mac OS X. In general, the difficulty of doing so is roughly on par with that on systems such as Linux and FreeBSD.

2.13.3. Interpreters

Several scripting languages are included in Mac OS X: AppleScript, Perl, PHP, Python,[72] Ruby, and Tcl. Multiple Unix shells are also included, such as bash, ksh, tcsh, and zsh. Mac OS X supports the Open Scripting Architecture (OSA), with AppleScript as the default (and only) installed language. Other languages for the OSA are available from third parties.

[72] Python on Mac OS X includes bindings to Core Graphics.

2.13.3.1. AppleScript

AppleScript is the preferred scripting language on Mac OS X, providing direct control of many parts of the system as well as applications. For example, using AppleScript, you can write scripts to automate operations, exchange data with applications, or send commands to applications. AppleScript can be used inand acrossall application environments. For application-specific actions to be performed using AppleScript, the application must explicitly support AppleScript. Such support typically requires a data model that lends itself well to being manipulated externally. However, generic operations (such as launching an application) are supported automatically. Figure 229 shows a trivial AppleScript program that speaks the operating system version. You can use either the osascript command-line tool or the AppleScript editor (/Applications/AppleScript/Script Editor.app) to run this program.

Figure 229. A trivial AppleScript program

-- osversion.scpt tell application "Finder"     set system_version to (get the version)     say "[[emph +]]Cool. This is Mac OS Ten" & system_version end tell

osascript executes a script file, which may be either a textual version or a compiled version of an AppleScript program. The osacompile command[73] can be used to compile source files, the standard input, or other compiled scripts into a single script.

[73] The osascript and osacompile commands will work with any installed scripting language that conforms to the OSA.

2.13.3.2. Automator

The Automator application is a visual tool for automating repetitive operations on Mac OS X. An Automator action is a modular unitan indivisible task from Automator's standpoint. For example, a task can create a directory, open a file, capture a screenshot, send an email message, or run a shell script. Multiple actions can be connected in a particular sequence to construct a workflow, which in turn is executed to perform the arbitrarily complex set of tasks that the workflow represents. An action may or may not require additional informationor argumentswhen it executes as part of a workflow. If additional information is required, the action displays a user interface consisting of text fields, checkboxes, buttons, pop-up menus, and so on. Automator includes a large number of predefined actions, but users can create their own actions using either AppleScript or Objective-C. A workflow is created visually by dragging or adding actions to a construction area. Finally, workflows can be saved for running later.

2.13.3.3. Command-Line Support

With each major release of Mac OS X, Apple has improved the system's command-line support by exposing more aspects of the system to be driven from the command line. In some cases, Apple has made command-line tools behave correctly and consistently. For example, in Mac OS X versions prior to 10.4, Unix-derived commands such as cp, mv, tar, and rsync did not handle certain aspects of Apple's HFS Plus file system correctly.[74] As still newer file system features such as metadata-based searching and access control lists (ACLs) were added in Mac OS X 10.4, the aforementioned commands were updated to behave consistently.

[74] These commands were not aware of HFS Plus resource forks until Mac OS X 10.4. We will look at details of HFS Plus in Chapter 12.

Let us consider a few other examples of using the command line in Mac OS X.

The drutil command can be used to interact with the Disc Recording framework (DiscRecording.framework), which manages CD and DVD burners. Figure 230 shows an example of its use.

Figure 230. Command-line interaction with the Disc Recording framework

$ drutil list    Vendor   Product           Rev   Bus       SupportLevel 1  HL-DT-ST DVD-RW GWA-4082B  C03D  ATAPI     Apple Shipping $ drutil getconfig current ... GetConfiguration returned 128 bytes.   00>  00 00 00 80 00 00 00 00 00 00 03 28 00 11 00 00   10>  00 14 00 00 00 13 00 00 00 1A 00 00 00 1B 00 00   20>  00 10 00 00 00 09 00 00 00 0A 00 00 00 08 00 00 ...         001Ah    DVD+RW                DVD ReWritable         001Bh    DVD+R                 DVD Recordable         0010h    DVD-ROM               Read only DVD ...

The hdiutil command interacts with the Disk Images framework (DiskImages.framework), which is used for accessing and manipulating disk images. Figure 231 shows an example of its use.

Figure 231. Command-line interaction with the Disk Images framework

$ hdiutil plugins      # Print information about plug-ins ... <dictionary> {    "plugin-key" = "CEncryptedEncoding"    "plugin-name" = "AES-128 (recommended)"    "plugin-class" = "CFileEncoding"    "plugin-type" = "builtin"    "plugin-encryption" = Yes } ... $ hdiutil burn foo.dmg # Burn image to an attached burning device ...

The say command uses the Speech Synthesis Manager to convert input text to audible speech. The resultant speech data may be either played back or saved to an AIFF file.

The sips[75] command provides basic image-processing functionality from the command line. It supports several image formats. Its goal is to allow quick and convenient desktop automation of common queries and operations on images. Figure 232 shows an example of using sips.

[75] sips stands for Scriptable Image Processing System.

Figure 232. Using the sips command to resample an image and convert its format

$ sips -g image.gif /private/tmp/image.gif pixelWidth: 1024   pixelHeight: 768   typeIdentifier: com.compuserve.gif   format: gif   formatOptions: default   dpiWidth: 72.000   dpiHeight: 72.000   samplesPerPixel: 4   bitsPerSample: 8   hasAlpha: yes   space: RGB   profile: Generic RGB Profile $ sips --resampleHeightWidth 640 480 -s format jpeg\         --out image.jpg /private/tmp/image.gif   /private/tmp/image.jpg

The Spotlight metadata search functionality is accessible from the command line. The mdls command lists the names and values of all metadata attributes associated with the specified files. The mdfind command can be used to find files matching a given query, optionally limiting the search to a specified directory. Moreover, mdfind can operate in "live" mode: It will continue to run until interrupted, while updating the number of matches. Figure 233 shows an example of using mdfind.

Figure 233. Using the mdfind command to find files matching a given query

$ mdfind -live "kMDItemFSName == 'foo.txt'" [type ctrl-C to exit] Query update: 1 matches # foo.txt created Query update: 0 matches # foo.txt deleted ... ^C $ mdfind "kMDItemContentType == 'com.adobe.pdf'" /Developer/About Xcode Tools.pdf ...

2.13.4. Tools

In addition to the development tools that are accessible through Xcode, Mac OS X provides a wide range of tools for analyzing, debugging, monitoring, profiling, and understanding both hardware and software.

Apple's general philosophy is to encourage programmers to use the highest possible level of abstraction as far as possible and to let the platform handle low-level details. This way, programmers can avoid using interfaces or system aspects that are likely to change during the evolution of Mac OS X. This approachparticularly when followed for end-user softwareis conducive to overall stability and a consistent user experience.


2.13.4.1. Debugging and Analysis Tools

The following are examples of debugging and analysis tools available on Mac OS X.

  • fs_usage reports system calls and page faults related to file system activity.

  • heap lists all malloc()-allocated buffers in a process's heap.

  • install_name_tool changes the dynamic shared library names installed in a Mach-O file.

  • ktrace enables kernel process tracing. kdump is used to view the resultant trace dump.

  • leaks searches a process's memory for unreferenced malloc() buffers.

  • lipo can create a multiple-architecture fat executable from one or more input files, list the architectures in a fat file, extract a single architecture file from a fat file, or create a fat file from an existing one, with the new file containing a subset of the architectures contained in the original.

  • lsof lists information about open files, where a file could be a regular file, a directory, a device file, a socket, and so on.

  • MallocDebug.app tracks and analyzes allocated memory.

  • malloc_history shows a process's malloc()-based allocations.

  • MergePef merges two or more PEF files into a single file.

  • ObjectAlloc.app TRacks Objective-C and Core Foundation object allocations and deallocations.

  • OpenGL Profiler.app is used for profiling OpenGL applications.

  • otool, as we have seen earlier, displays various parts of an object file.

  • pagestuff displays information on specified pages of a Mach-O file.

  • PEFViewer displays the contents of a PEF binary.

  • QuartzDebug.app is a visualizer for an application's screen-drawing behaviorit briefly flashes the areas that are being redrawn. It also allows you to alter the user interface's scale factor and enable or disable graphics hardware acceleration.

  • sample profiles a process during a given time interval.

  • Sampler.app is a viewer for a program's execution behavior.

  • sc_usage shows system call usage statistics.

  • Spin Control.app samples applications that fail to respond quickly enough, causing the spinning cursor to appear.

  • Thread Viewer.app is a viewer for threads and thread activity.

  • vmmap displays virtual memory regions in a process.

  • vm_stat displays Mach virtual memory statistics.

2.13.4.2. Computer Hardware Understanding Development Tools

The Computer Hardware Understanding Development (CHUD) package is a set of low-level tools that can be optionally installed on Mac OS X. CHUD tools include the following specific programs.

  • BigTop.app is a graphical equivalent to command-line tools such as top and vm_stat. It displays a variety of system statistics.

  • CacheBasher.app is a tool for measuring cache performance.

  • MONster.app is a tool for collecting and visualizing hardware-level performance data.

  • PMC Index.app is a tool for searching Performance Monitoring Counter (PMC) events.

  • Reggie SE.app is a viewer and editor for CPU and PCI configuration registers.

  • Saturn.app is a tool for profiling applications at the function-call level. It is also used for visualizing the profile data.

  • Shark.app performs system-wide sampling and profiling to create a profile of a program's execution behavior. This helps the programmer understand where time is being spent as the code runs.

  • Skidmarks GT.app is a processor-performance benchmarking tool. It supports integer, floating-point, and vector benchmarks.

  • SpindownHD.app is a utility for displaying the sleep/active status of attached drives.

  • amber traces all threads of execution in a process, recording every instruction and data access to a trace file. acid analyzes traces generated by amber.

  • simg4 is a cycle-accurate core simulator of the Motorola PowerPC G4 processor.

  • simg5 is a cycle-accurate core simulator of the IBM PowerPC 970 (G5) processor.

2.13.4.3. Visual Tools

Mac OS X also provides several visual design and programming tools, most of which we came across earlier in this chapter, for example, AppleScript Studio, Automator, AU Lab, Interface Builder, Quartz Composer, and Xcode class- and data-modeling tools.




Mac OS X Internals. A Systems Approach
Mac OS X Internals: A Systems Approach
ISBN: 0321278542
EAN: 2147483647
Year: 2006
Pages: 161
Authors: Amit Singh

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net