xref: /aosp_15_r20/external/webrtc/test/testsupport/mac_file_utils.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
2 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import <Foundation/Foundation.h>
12#include <dlfcn.h>
13#include <mach-o/dyld.h>
14#include <stdint.h>
15#include <stdlib.h>
16
17#include "rtc_base/checks.h"
18
19namespace webrtc {
20namespace test {
21
22void GetNSExecutablePath(std::string* path) {
23  RTC_DCHECK(path);
24  // Executable path can have relative references ("..") depending on
25  // how the app was launched.
26  uint32_t executable_length = 0;
27  _NSGetExecutablePath(NULL, &executable_length);
28  RTC_DCHECK_GT(executable_length, 1u);
29  char executable_path[PATH_MAX + 1];
30  int rv = _NSGetExecutablePath(executable_path, &executable_length);
31  RTC_DCHECK_EQ(rv, 0);
32
33  char full_path[PATH_MAX];
34  if (realpath(executable_path, full_path) == nullptr) {
35    *path = "";
36    return;
37  }
38
39  *path = full_path;
40}
41
42}  // namespace test
43}  // namespace webrtc
44