Skip to content

Commit a227033

Browse files
committed
Add .mm files necessary to build on OS X
1 parent d4950e3 commit a227033

11 files changed

+1466
-0
lines changed

Diff for: CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
198198
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
199199
FIND_LIBRARY(COREFOUNDATION_LIBRARY CoreFoundation)
200200
FIND_LIBRARY(COCOA_LIBRARY Cocoa)
201+
FIND_LIBRARY(SECURITY_LIBRARY Security)
201202
set(
202203
BASE_ARCH_LIBRARIES
203204

@@ -208,6 +209,9 @@ elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
208209
BASE_ARCH_SOURCES
209210

210211
src/base/threading/platform_thread_mac.mm
212+
src/base/strings/sys_string_conversions_mac.mm
213+
src/base/mac/foundation_util.mm
214+
src/base/mac/bundle_locations.mm
211215
src/base/mac/mach_logging.cc
212216
src/base/mac/scoped_mach_port.cc
213217
src/base/time/time_mac.cc

Diff for: DEPS

+9
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@
114114
"base/base_paths_win.h",
115115
"base/message_loop/message_loop.h"
116116
]
117+
},
118+
{
119+
"from": "base/strings/sys_string_conversions_mac.mm",
120+
"exclude": [
121+
"base/debug/debugger.h",
122+
"base/sequence_checker.h",
123+
"base/files/file.h",
124+
"base/tracked_objects.h"
125+
]
117126
}
118127
],
119128
"manual_dependency": [

Diff for: getdep.py

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def enq(new, now):
5252
enq(now[:-1] + 'cc', now)
5353
depmap[now].append(now[:-1] + 'cc')
5454

55+
if now.endswith(".h") and os.path.exists(self.realpath(now[:-1] + 'mm')):
56+
enq(now[:-1] + 'mm', now)
57+
depmap[now].append(now[:-1] + 'mm')
58+
5559
for dependency in self.parse_cc(now):
5660
enq(dependency, now)
5761
# print now, dependency

Diff for: src/base/base_paths_mac.mm

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac
6+
// in base/path_service.cc.
7+
8+
#include <dlfcn.h>
9+
#import <Foundation/Foundation.h>
10+
#include <mach-o/dyld.h>
11+
12+
#include "base/base_paths.h"
13+
#include "base/compiler_specific.h"
14+
#include "base/files/file_path.h"
15+
#include "base/files/file_util.h"
16+
#include "base/logging.h"
17+
#include "base/mac/foundation_util.h"
18+
#include "base/path_service.h"
19+
#include "base/strings/string_util.h"
20+
#include "build/build_config.h"
21+
22+
namespace {
23+
24+
void GetNSExecutablePath(base::FilePath* path) {
25+
DCHECK(path);
26+
// Executable path can have relative references ("..") depending on
27+
// how the app was launched.
28+
uint32_t executable_length = 0;
29+
_NSGetExecutablePath(NULL, &executable_length);
30+
DCHECK_GT(executable_length, 1u);
31+
std::string executable_path;
32+
int rv = _NSGetExecutablePath(
33+
base::WriteInto(&executable_path, executable_length),
34+
&executable_length);
35+
DCHECK_EQ(rv, 0);
36+
37+
// _NSGetExecutablePath may return paths containing ./ or ../ which makes
38+
// FilePath::DirName() work incorrectly, convert it to absolute path so that
39+
// paths such as DIR_SOURCE_ROOT can work, since we expect absolute paths to
40+
// be returned here.
41+
*path = base::MakeAbsoluteFilePath(base::FilePath(executable_path));
42+
}
43+
44+
// Returns true if the module for |address| is found. |path| will contain
45+
// the path to the module. Note that |path| may not be absolute.
46+
bool GetModulePathForAddress(base::FilePath* path,
47+
const void* address) WARN_UNUSED_RESULT;
48+
49+
bool GetModulePathForAddress(base::FilePath* path, const void* address) {
50+
Dl_info info;
51+
if (dladdr(address, &info) == 0)
52+
return false;
53+
*path = base::FilePath(info.dli_fname);
54+
return true;
55+
}
56+
57+
} // namespace
58+
59+
namespace base {
60+
61+
bool PathProviderMac(int key, base::FilePath* result) {
62+
switch (key) {
63+
case base::FILE_EXE:
64+
GetNSExecutablePath(result);
65+
return true;
66+
case base::FILE_MODULE:
67+
return GetModulePathForAddress(result,
68+
reinterpret_cast<const void*>(&base::PathProviderMac));
69+
case base::DIR_APP_DATA: {
70+
bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
71+
result);
72+
#if defined(OS_IOS)
73+
// On IOS, this directory does not exist unless it is created explicitly.
74+
if (success && !base::PathExists(*result))
75+
success = base::CreateDirectory(*result);
76+
#endif // defined(OS_IOS)
77+
return success;
78+
}
79+
case base::DIR_SOURCE_ROOT:
80+
// Go through PathService to catch overrides.
81+
if (!PathService::Get(base::FILE_EXE, result))
82+
return false;
83+
84+
// Start with the executable's directory.
85+
*result = result->DirName();
86+
87+
#if !defined(OS_IOS)
88+
if (base::mac::AmIBundled()) {
89+
// The bundled app executables (Chromium, TestShell, etc) live five
90+
// levels down, eg:
91+
// src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
92+
*result = result->DirName().DirName().DirName().DirName().DirName();
93+
} else {
94+
// Unit tests execute two levels deep from the source root, eg:
95+
// src/xcodebuild/{Debug|Release}/base_unittests
96+
*result = result->DirName().DirName();
97+
}
98+
#endif
99+
return true;
100+
case base::DIR_USER_DESKTOP:
101+
#if defined(OS_IOS)
102+
// iOS does not have desktop directories.
103+
NOTIMPLEMENTED();
104+
return false;
105+
#else
106+
return base::mac::GetUserDirectory(NSDesktopDirectory, result);
107+
#endif
108+
case base::DIR_CACHE:
109+
return base::mac::GetUserDirectory(NSCachesDirectory, result);
110+
default:
111+
return false;
112+
}
113+
}
114+
115+
} // namespace base

Diff for: src/base/mac/bundle_locations.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef BASE_MAC_BUNDLE_LOCATIONS_H_
6+
#define BASE_MAC_BUNDLE_LOCATIONS_H_
7+
8+
#include "base/base_export.h"
9+
#include "base/files/file_path.h"
10+
11+
#if defined(__OBJC__)
12+
#import <Foundation/Foundation.h>
13+
#else // __OBJC__
14+
class NSBundle;
15+
class NSString;
16+
#endif // __OBJC__
17+
18+
namespace base {
19+
20+
class FilePath;
21+
22+
namespace mac {
23+
24+
// This file provides several functions to explicitly request the various
25+
// component bundles of Chrome. Please use these methods rather than calling
26+
// +[NSBundle mainBundle] or CFBundleGetMainBundle().
27+
//
28+
// Terminology
29+
// - "Outer Bundle" - This is the main bundle for Chrome; it's what
30+
// +[NSBundle mainBundle] returns when Chrome is launched normally.
31+
//
32+
// - "Main Bundle" - This is the bundle from which Chrome was launched.
33+
// This will be the same as the outer bundle except when Chrome is launched
34+
// via an app shortcut, in which case this will return the app shortcut's
35+
// bundle rather than the main Chrome bundle.
36+
//
37+
// - "Framework Bundle" - This is the bundle corresponding to the Chrome
38+
// framework.
39+
//
40+
// Guidelines for use:
41+
// - To access a resource, the Framework bundle should be used.
42+
// - If the choice is between the Outer or Main bundles then please choose
43+
// carefully. Most often the Outer bundle will be the right choice, but for
44+
// cases such as adding an app to the "launch on startup" list, the Main
45+
// bundle is probably the one to use.
46+
47+
// Methods for retrieving the various bundles.
48+
BASE_EXPORT NSBundle* MainBundle();
49+
BASE_EXPORT FilePath MainBundlePath();
50+
BASE_EXPORT NSBundle* OuterBundle();
51+
BASE_EXPORT FilePath OuterBundlePath();
52+
BASE_EXPORT NSBundle* FrameworkBundle();
53+
BASE_EXPORT FilePath FrameworkBundlePath();
54+
55+
// Set the bundle that the preceding functions will return, overriding the
56+
// default values. Restore the default by passing in |nil|.
57+
BASE_EXPORT void SetOverrideOuterBundle(NSBundle* bundle);
58+
BASE_EXPORT void SetOverrideFrameworkBundle(NSBundle* bundle);
59+
60+
// Same as above but accepting a FilePath argument.
61+
BASE_EXPORT void SetOverrideOuterBundlePath(const FilePath& file_path);
62+
BASE_EXPORT void SetOverrideFrameworkBundlePath(const FilePath& file_path);
63+
64+
} // namespace mac
65+
} // namespace base
66+
67+
#endif // BASE_MAC_BUNDLE_LOCATIONS_H_

Diff for: src/base/mac/bundle_locations.mm

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "base/mac/bundle_locations.h"
6+
7+
#include "base/logging.h"
8+
#include "base/mac/foundation_util.h"
9+
#include "base/strings/sys_string_conversions.h"
10+
11+
namespace base {
12+
namespace mac {
13+
14+
// NSBundle isn't threadsafe, all functions in this file must be called on the
15+
// main thread.
16+
static NSBundle* g_override_framework_bundle = nil;
17+
static NSBundle* g_override_outer_bundle = nil;
18+
19+
NSBundle* MainBundle() {
20+
return [NSBundle mainBundle];
21+
}
22+
23+
FilePath MainBundlePath() {
24+
NSBundle* bundle = MainBundle();
25+
return NSStringToFilePath([bundle bundlePath]);
26+
}
27+
28+
NSBundle* OuterBundle() {
29+
if (g_override_outer_bundle)
30+
return g_override_outer_bundle;
31+
return [NSBundle mainBundle];
32+
}
33+
34+
FilePath OuterBundlePath() {
35+
NSBundle* bundle = OuterBundle();
36+
return NSStringToFilePath([bundle bundlePath]);
37+
}
38+
39+
NSBundle* FrameworkBundle() {
40+
if (g_override_framework_bundle)
41+
return g_override_framework_bundle;
42+
return [NSBundle mainBundle];
43+
}
44+
45+
FilePath FrameworkBundlePath() {
46+
NSBundle* bundle = FrameworkBundle();
47+
return NSStringToFilePath([bundle bundlePath]);
48+
}
49+
50+
static void AssignOverrideBundle(NSBundle* new_bundle,
51+
NSBundle** override_bundle) {
52+
if (new_bundle != *override_bundle) {
53+
[*override_bundle release];
54+
*override_bundle = [new_bundle retain];
55+
}
56+
}
57+
58+
static void AssignOverridePath(const FilePath& file_path,
59+
NSBundle** override_bundle) {
60+
NSString* path = base::SysUTF8ToNSString(file_path.value());
61+
NSBundle* new_bundle = [NSBundle bundleWithPath:path];
62+
DCHECK(new_bundle) << "Failed to load the bundle at " << file_path.value();
63+
AssignOverrideBundle(new_bundle, override_bundle);
64+
}
65+
66+
void SetOverrideOuterBundle(NSBundle* bundle) {
67+
AssignOverrideBundle(bundle, &g_override_outer_bundle);
68+
}
69+
70+
void SetOverrideFrameworkBundle(NSBundle* bundle) {
71+
AssignOverrideBundle(bundle, &g_override_framework_bundle);
72+
}
73+
74+
void SetOverrideOuterBundlePath(const FilePath& file_path) {
75+
AssignOverridePath(file_path, &g_override_outer_bundle);
76+
}
77+
78+
void SetOverrideFrameworkBundlePath(const FilePath& file_path) {
79+
AssignOverridePath(file_path, &g_override_framework_bundle);
80+
}
81+
82+
} // namespace mac
83+
} // namespace base

0 commit comments

Comments
 (0)