Finding os version at compile time and os version at runtime?

  • Hi,

    I have an app I am trying to maintain with one set of sources.

    The app uses the quartz filter manager and some leopard pdfkit
    stuff that is not available in the 10.4 universal sdk.

    I would like to be able to detect the target os version at
    compile time so I can prevent the 10.5 stuff from compiling
    when I am building a universal version. ( It is all in
    a single subroutine).

    I also need to be able to detect at runtime if the app
    is running a version < 10.5 so I can disable the menu
    that invokes the 10.5 code.

    Is there a standard way for doing both of these tasks?

    Thanks,

    Jerry
  • On Dec 15, 2007, at 8:06 PM, Jerry LeVan wrote:

    > Hi,
    >
    > I have an app I am trying to maintain with one set of sources.
    >
    > The app uses the quartz filter manager and some leopard pdfkit
    > stuff that is not available in the 10.4 universal sdk.
    >
    > I would like to be able to detect the target os version at
    > compile time so I can prevent the 10.5 stuff from compiling
    > when I am building a universal version. ( It is all in
    > a single subroutine).
    >
    > I also need to be able to detect at runtime if the app
    > is running a version < 10.5 so I can disable the menu
    > that invokes the 10.5 code.
    >
    > Is there a standard way for doing both of these tasks?

    I've had to deal with this a bit myself.  You can see the results in
    my open source project at http://regexkit.sourceforge.net/
    Specifically, you'll want to examine the file that's in 'Source/Build/
    Xcode/RegexKit Build Settings.xcconfig', which I use to keep a lot of
    the projects Xcode tunables where they can be commented, instead of
    inside the .xcodeproj project.  I source the .xcconfig file at the
    project level by setting the Project > Edit Project Settings > Build
    (tab) > Based On (drop down menu, lower right).

    Here's a few examples.  You can create the exact equivalents inside
    Xcode's Project > Edit (Project | Target | Executable) Settings.

    // The SDK for the current Mac OS X release

    SDKROOT_1040                    = /Developer/SDKs/MacOSX10.4u.sdk
    SDKROOT_1050                    = /Developer/SDKs/MacOSX10.5.sdk
    SDKROOT                        = ${SDKROOT_${MAC_OS_X_VERSION_MAJOR}}

    // Compiler flags dependent on the current Mac OS X release

    XCODE_CFLAGS_1040              =
    XCODE_CFLAGS_1050              = -fstack-protector-all
    XCODE_CFLAGS_FOR_OS            = ${XCODE_CFLAGS_$
    {MAC_OS_X_VERSION_MAJOR}}
    OTHER_CFLAGS                    = ${XCODE_CFLAGS_FOR_OS}

    // The architectures we build for for the current Mac OS X release

    ARCHS_1040                      = ppc i386
    ARCHS_1050                      = ppc ppc64 i386 x86_64
    ARCHS                          = ${ARCHS_${MAC_OS_X_VERSION_MAJOR}}

    // Xcode 3.0 will apply per architecture settings for _ARCH build
    settings

    MACOSX_DEPLOYMENT_TARGET_ppc    = 10.4
    MACOSX_DEPLOYMENT_TARGET_i386  = 10.4
    MACOSX_DEPLOYMENT_TARGET_ppc64  = 10.5
    MACOSX_DEPLOYMENT_TARGET_x86_64 = 10.5

    As to the detection of 10.4 and 10.5 functionality at run time with
    code, that can be a bit trickier depending on what you're attempting
    to do.  And there's a lot of different ways to accomplish it.  I would
    suggest taking a look at:

    http://developer.apple.com/documentation/DeveloperTools/Conceptual/DynamicL
    ibraries/Introduction.html

    http://developer.apple.com/documentation/DeveloperTools/Conceptual/DynamicL
    ibraries/Articles/DynamicLibraryUsageGuidelines.html#/

    /apple_ref/doc/uid/TP40001928-SW13 (Using Weakly Linked Symbols)

    With Objective-C, there's some additional possibilities.  You can ask
    the runtime system to check if a class exists, or if an object
    responds to a selector.  As an example:

        if([objc_getClass("NSGarbageCollector") defaultCollector] !=
    NULL) { /* NSGarbageCollector exists, and Garbage Collection is
    active. */ }

        if([NSThread respondsToSelector(@selector(mainThread))]) { /*
    NSThread responds to the new 10.5 mainThread method. */ }
  • I made a little class once that gets the OS version at runtime. It
    worked on Tiger, haven't used it for a long time though.

    + (SInt32)macOSVersion
    {
    SInt32 MacVersion;

    if (Gestalt(gestaltSystemVersion, &MacVersion) == noErr) {
      return MacVersion;
    }
    else
      return 0;
    }

    The Gestalt function is a carbon function, so I guess it won't work on
    leopard.

    On 16/12/2007, at 12:06 PM, Jerry LeVan wrote:

    > Hi,
    >
    > I have an app I am trying to maintain with one set of sources.
    >
    > The app uses the quartz filter manager and some leopard pdfkit
    > stuff that is not available in the 10.4 universal sdk.
    >
    > I would like to be able to detect the target os version at
    > compile time so I can prevent the 10.5 stuff from compiling
    > when I am building a universal version. ( It is all in
    > a single subroutine).
    >
    > I also need to be able to detect at runtime if the app
    > is running a version < 10.5 so I can disable the menu
    > that invokes the 10.5 code.
    >
    > Is there a standard way for doing both of these tasks?
    >
    > Thanks,
    >
    > Jerry
  • On Dec 15, 2007, at 9:48 PM, John Engelhart wrote:

    > On Dec 15, 2007, at 8:06 PM, Jerry LeVan wrote:
    >
    >> Hi,
    >>
    >> I have an app I am trying to maintain with one set of sources.
    >>
    >> The app uses the quartz filter manager and some leopard pdfkit
    >> stuff that is not available in the 10.4 universal sdk.
    >>
    >> I would like to be able to detect the target os version at
    >> compile time so I can prevent the 10.5 stuff from compiling
    >> when I am building a universal version. ( It is all in
    >> a single subroutine).
    >>
    >> I also need to be able to detect at runtime if the app
    >> is running a version < 10.5 so I can disable the menu
    >> that invokes the 10.5 code.
    >>
    >> Is there a standard way for doing both of these tasks?
    >
    > I've had to deal with this a bit myself.  You can see the results in
    > my open source project athttp://regexkit.sourceforge.net/
    > Specifically, you'll want to examine the file that's in 'Source/
    > Build/Xcode/RegexKit Build Settings.xcconfig', which I use to keep a
    > lot of the projects Xcode tunables where they can be commented,
    > instead of inside the .xcodeproj project.  I source the .xcconfig
    > file at the project level by setting the Project > Edit Project
    > Settings > Build (tab) > Based On (drop down menu, lower right).

    John,

    While I chew on what you said...

    I have a temp workaround.

    In the universal build "parameters" section I simply added an
    addition cflag "-DUNIVERSAL" and then #ifdef'ed the appropriate
    hunk(s) of code.

    In the nonuniversal build (Leopard) I did not define the additional
    cflag.

    I think I will be able to use the respondToSelector for runtime
    considerations.

    Thanks,
    Jerry
  • On 15 Dec 07, at 18:59, Nik Youdale wrote:
    > I made a little class once that gets the OS version at runtime. It
    > worked on Tiger, haven't used it for a long time though.
    >
    > + (SInt32)macOSVersion
    > {
    > SInt32 MacVersion;
    >
    > if (Gestalt(gestaltSystemVersion, &MacVersion) == noErr) {
    > return MacVersion;
    > }
    > else
    > return 0;
    > }
    >
    > The Gestalt function is a carbon function, so I guess it won't work
    > on leopard.

    Carbon is still available under Leopard. The only limitation is that
    certain parts of Carbon (primarily HIToolbox) aren't available in 64-
    bit applications.
  • On 16/12/2007, at 2:49 PM, Andrew Farmer wrote:

    > Carbon is still available under Leopard. The only limitation is that
    > certain parts of Carbon (primarily HIToolbox) aren't available in 64-
    > bit applications.

    But is it updated? I had a quick read of the documentation of the
    gestaltSystemVersion selector, and noticed it didnt mention anything
    about OS X 10.5, it only listed 10.0 - 10.4.
  • On Dec 15, 2007, at 11:14 PM, Nik Youdale wrote:

    > On 16/12/2007, at 2:49 PM, Andrew Farmer wrote:
    >
    >> Carbon is still available under Leopard. The only limitation is
    >> that certain parts of Carbon (primarily HIToolbox) aren't available
    >> in 64-bit applications.
    >
    > But is it updated? I had a quick read of the documentation of the
    > gestaltSystemVersion selector, and noticed it didnt mention anything
    > about OS X 10.5, it only listed 10.0 - 10.4.

    It works fine on 10.5.x.

    - d
  • I think people might complain if Carbon apps like Photoshop,
    Microsoft Word, and World of Warcraft stopped running in Leopard ;)
    Carbon is still supported fine. It's just not moving to 64-bit. And
    not really recommended for new development.

    On Dec 15, 2007, at 8:18 PM, Dave Hersey wrote:

    > On Dec 15, 2007, at 11:14 PM, Nik Youdale wrote:
    >
    >> On 16/12/2007, at 2:49 PM, Andrew Farmer wrote:
    >>
    >>> Carbon is still available under Leopard. The only limitation is
    >>> that certain parts of Carbon (primarily HIToolbox) aren't
    >>> available in 64-bit applications.
    >>
    >> But is it updated? I had a quick read of the documentation of the
    >> gestaltSystemVersion selector, and noticed it didnt mention
    >> anything about OS X 10.5, it only listed 10.0 - 10.4.
    >
    > It works fine on 10.5.x.
    >
    > - d
  • On 16/12/2007, at 1:59 PM, Nik Youdale wrote:

    > The Gestalt function is a carbon function, so I guess it won't work
    > on leopard.

    Both of these are actually not true, the Gestalt function is included
    even if you just link against Cocoa/Cocoa.h and it definitely works on
    Leopard.

    You should actually use something like this to get the full OS version
    number:

    long majorVersion, minorVersion, bugFixVersion;

    Gestalt(gestaltSystemVersionMajor, &majorVersion);
    Gestalt(gestaltSystemVersionMinor, &minorVersion);
    Gestalt(gestaltSystemVersionBugFix, &bugFixVersion);

    NSLog(@"Running on Mac OS X %d.%d.
    %d",majorVersion,minorVersion,bugFixVersion);

    A lot of runtime checks broke when Apple released Mac OS X 10.4.10
    because the bug fix version was assumed to be always a single digit.
    Getting the individual version numbers as above will always work.

    --
    Rob Keniger
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today