blob: d4354d80b0e3d761ce38194be5eaf9cb0b71341a [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
andrew@webrtc.org7a281a52012-06-27 03:22:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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
pbos@webrtc.org34741c82013-05-27 08:02:22 +000011#include "webrtc/test/testsupport/fileutils.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000012
13#ifdef WIN32
14#include <direct.h>
15#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#else
17#include <unistd.h>
18#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000019#endif
20
21#include <sys/stat.h> // To check for directory existence.
22#ifndef S_ISDIR // Not defined in stat.h on Windows.
23#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024#endif
25
26#include <cstdio>
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000027#include <cstring>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000028
pbos@webrtc.org34741c82013-05-27 08:02:22 +000029#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000030
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000031namespace webrtc {
32namespace test {
33
henrike@webrtc.org34773d92013-07-08 14:55:23 +000034namespace {
35
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000036#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000037const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000038#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000039const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000040#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000041
42#ifdef WEBRTC_ANDROID
henrike@webrtc.org34773d92013-07-08 14:55:23 +000043const char* kResourcesDirName = "resources";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000044#else
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000045// The file we're looking for to identify the project root dir.
henrike@webrtc.org34773d92013-07-08 14:55:23 +000046const char* kProjectRootFileName = "DEPS";
47const char* kResourcesDirName = "resources";
leozwang@webrtc.orgfb594422012-06-29 18:28:12 +000048#endif
henrike@webrtc.orgcaf2fcc2013-07-05 04:15:38 +000049
henrike@webrtc.org34773d92013-07-08 14:55:23 +000050const char* kFallbackPath = "./";
51const char* kOutputDirName = "out";
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000052char relative_dir_path[FILENAME_MAX];
53bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000054
55} // namespace
56
57const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
58
59std::string OutputPathAndroid();
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +000060std::string ProjectRootPathAndroid();
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000061
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000062void SetExecutablePath(const std::string& path) {
63 std::string working_dir = WorkingDir();
64 std::string temp_path = path;
65
66 // Handle absolute paths; convert them to relative paths to the working dir.
67 if (path.find(working_dir) != std::string::npos) {
68 temp_path = path.substr(working_dir.length() + 1);
69 }
70 // Trim away the executable name; only store the relative dir path.
71 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000072 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
73 relative_dir_path_set = true;
74}
75
76bool FileExists(std::string& file_name) {
77 struct stat file_info = {0};
78 return stat(file_name.c_str(), &file_info) == 0;
79}
80
henrike@webrtc.org34773d92013-07-08 14:55:23 +000081std::string OutputPathImpl() {
82 std::string path = ProjectRootPath();
83 if (path == kCannotFindProjectRootDir) {
84 return kFallbackPath;
85 }
86 path += kOutputDirName;
87 if (!CreateDirectory(path)) {
88 return kFallbackPath;
89 }
90 return path + kPathDelimiter;
91}
92
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000093#ifdef WEBRTC_ANDROID
94
95std::string ProjectRootPath() {
pbos@webrtc.orgfc496d92013-07-09 15:24:16 +000096 return ProjectRootPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000097}
98
99std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000100 return OutputPathAndroid();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000101}
102
103std::string WorkingDir() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000104 return ProjectRootPath();
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000105}
106
107#else // WEBRTC_ANDROID
108
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000109std::string ProjectRootPath() {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000110 std::string path = WorkingDir();
111 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000112 return kCannotFindProjectRootDir;
113 }
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000114 if (relative_dir_path_set) {
115 path = path + kPathDelimiter + relative_dir_path;
116 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000117 // Check for our file that verifies the root dir.
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000118 size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
119 while (path_delimiter_index != std::string::npos) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000120 std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
121 if (FileExists(root_filename)) {
122 return path + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000123 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000124 // Move up one directory in the directory tree.
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000125 path = path.substr(0, path_delimiter_index);
126 path_delimiter_index = path.find_last_of(kPathDelimiter);
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000127 }
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000128 // Reached the root directory.
129 fprintf(stderr, "Cannot find project root directory!\n");
130 return kCannotFindProjectRootDir;
131}
132
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000133std::string OutputPath() {
henrike@webrtc.org34773d92013-07-08 14:55:23 +0000134 return OutputPathImpl();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000135}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000136
137std::string WorkingDir() {
138 char path_buffer[FILENAME_MAX];
139 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
140 fprintf(stderr, "Cannot get current directory!\n");
141 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000142 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000143 return std::string(path_buffer);
144 }
145}
146
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000147#endif // !WEBRTC_ANDROID
148
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000149bool CreateDirectory(std::string directory_name) {
150 struct stat path_info = {0};
151 // Check if the path exists already:
152 if (stat(directory_name.c_str(), &path_info) == 0) {
153 if (!S_ISDIR(path_info.st_mode)) {
154 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
155 "file and re-run to create the directory.\n",
156 directory_name.c_str());
157 return false;
158 }
159 } else {
160#ifdef WIN32
161 return _mkdir(directory_name.c_str()) == 0;
162#else
163 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
164#endif
165 }
166 return true;
167}
168
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000169std::string ResourcePath(std::string name, std::string extension) {
170 std::string platform = "win";
171#ifdef WEBRTC_LINUX
172 platform = "linux";
173#endif // WEBRTC_LINUX
174#ifdef WEBRTC_MAC
175 platform = "mac";
176#endif // WEBRTC_MAC
177
178#ifdef WEBRTC_ARCH_64_BITS
179 std::string architecture = "64";
180#else
181 std::string architecture = "32";
182#endif // WEBRTC_ARCH_64_BITS
183
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000184 std::string resources_path = ProjectRootPath() + kResourcesDirName +
185 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000186 std::string resource_file = resources_path + name + "_" + platform + "_" +
187 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000188 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000189 return resource_file;
190 }
191 // Try without architecture.
192 resource_file = resources_path + name + "_" + platform + "." + extension;
193 if (FileExists(resource_file)) {
194 return resource_file;
195 }
196 // Try without platform.
197 resource_file = resources_path + name + "_" + architecture + "." + extension;
198 if (FileExists(resource_file)) {
199 return resource_file;
200 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000201
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000202 // Fall back on name without architecture or platform.
203 return resources_path + name + "." + extension;
204}
205
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000206size_t GetFileSize(std::string filename) {
207 FILE* f = fopen(filename.c_str(), "rb");
208 size_t size = 0;
209 if (f != NULL) {
210 if (fseek(f, 0, SEEK_END) == 0) {
211 size = ftell(f);
212 }
213 fclose(f);
214 }
215 return size;
216}
217
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000218} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000219} // namespace webrtc