Skip to content

Commit 601c199

Browse files
author
Jiangyong Chen
committed
增加cmake编译,mac上编译测试通过
1 parent 152a449 commit 601c199

8 files changed

+77
-13
lines changed

DexPatcher/Build.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "Build.h"
22
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#ifdef _WIN32
36
#include <Windows.h>
7+
#endif
48

59
void GetBuildDateBase(char* year, char* month, char* day)
610
{
@@ -17,7 +21,7 @@ char* Build::GetBuildDate(void)
1721
{
1822
char year, month, day;
1923
GetBuildDateBase(&year, &month, &day);//取编译时间
20-
sprintf_s(g_dateBuffer, "20%02d.%02d.%02d", year, month, day);//任意格式化
24+
sprintf(g_dateBuffer, "20%02d.%02d.%02d", year, month, day);//任意格式化
2125
return g_dateBuffer;
2226
}
2327

@@ -48,4 +52,4 @@ void getBuildTime()
4852
default:month = 0; break;
4953
}*/
5054

51-
}
55+
}

DexPatcher/CMakeLists.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# set the project name
4+
project(DexPatcher VERSION 0.1.0)
5+
6+
include(CheckCXXCompilerFlag)
7+
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
8+
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
9+
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
10+
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
11+
add_compile_definitions(__STDC_LIB_EXT1__)
12+
add_definitions(-D__STDC_LIB_EXT1__)
13+
add_definitions(-D__STDC_WANT_LIB_EXT1__)
14+
if(COMPILER_SUPPORTS_CXX17)
15+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
16+
set(CMAKE_CXX_STANDARD 17)
17+
elseif(COMPILER_SUPPORTS_CXX14)
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
19+
elseif(COMPILER_SUPPORTS_CXX11)
20+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
21+
elseif(COMPILER_SUPPORTS_CXX0X)
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
23+
else()
24+
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
25+
endif()
26+
27+
# add the executable
28+
add_executable(DexPatcher main.cpp Build.cpp Command.cpp ParseDex.cpp PatchDex.cpp Utils.cpp)

DexPatcher/ParseDex.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool ParseDex::openDexFile(const char* filePath)
4343
{
4444
return false;
4545
}
46-
strcpy_s(mFilePath, MAX_PATH, filePath);
46+
strncpy(mFilePath, filePath, MAX_PATH);
4747

4848
// 保存带后缀的文件名
4949
mFileName = (char*)malloc(MAX_PATH);
@@ -74,7 +74,7 @@ bool ParseDex::saveDexFile()
7474

7575
// 构造保存文件名
7676
char fileName[MAX_PATH] = { 0 };
77-
sprintf_s(fileName, "%s%s_patched.dex", path, fileNameWithoutExtension);
77+
sprintf(fileName, "%s%s_patched.dex", path, fileNameWithoutExtension);
7878

7979
// 保存文件
8080
bool result = File::saveFile(mDexBuffer, fileName, mFileSize);

DexPatcher/ParseDex.h

+8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
#pragma once
1616
#include "DexFile.h"
17+
#ifdef _WIN32
1718
#include <Windows.h>
19+
#else
20+
#include <errno.h>
21+
#endif
1822
#include <vector>
1923
#include <string>
2024

@@ -43,8 +47,12 @@ class ParseDex
4347
{
4448
if (!openDexFile(filePath))
4549
{
50+
#ifdef _WIN32
4651
int error = GetLastError();
4752
printf("openDexFile failed:%d\n", error);
53+
#else
54+
printf("openDexFile failed:%d\n", errno);
55+
#endif
4856
}
4957
}
5058
~ParseDex()

DexPatcher/PatchDex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void PatchDex::fixMethod(string methodInfoPath, bool noLog)
9696

9797

9898
char strProgress[64] = { 0 };
99-
sprintf_s(strProgress,"[%3d%% %d/%d] ", (int)(getProgress() * 100), i + 1,classDefMethods.size());
99+
sprintf(strProgress,"[%3d%% %d/%d] ", (int)(getProgress() * 100), i + 1,classDefMethods.size());
100100
string strTitle = strProgress;
101101
strTitle += methodName;
102102
if (!noLog)

DexPatcher/Utils.h

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
#pragma once
22

3-
#define WINDOWS
4-
3+
#include "config.h"
54

65

76
#include <string.h>
87
#include "DexFile.h"
98
#include <string>
10-
11-
#ifdef WINDOWS
9+
#include <errno.h>
10+
#ifdef _WIN32
1211
#include <Windows.h>
12+
#else
13+
inline errno_t fopen_s(FILE **f, const char *name, const char *mode) {
14+
errno_t ret = 0;
15+
assert(f);
16+
*f = fopen(name, mode);
17+
/* Can't be sure about 1-to-1 mapping of errno and MS' errno_t */
18+
if (!*f)
19+
ret = errno;
20+
return ret;
21+
}
1322
#endif
1423

1524

@@ -96,7 +105,7 @@ namespace Utils
96105
j = i + 1;
97106
}
98107
}
99-
strcpy_s(fileName, MAX_PATH, &filePath[j]);
108+
strncpy(fileName, &filePath[j], MAX_PATH);
100109
}
101110

102111
/// <summary>
@@ -118,7 +127,7 @@ namespace Utils
118127
j = i + 1;
119128
}
120129
}
121-
strcpy_s(path, MAX_PATH, filePath);
130+
strncpy(path, filePath, MAX_PATH);
122131
memcpy(path + j, "\0", 1);
123132
}
124133

@@ -136,7 +145,7 @@ namespace Utils
136145
int i = 0;
137146
for (i = 0; i < strlen(fileName); i++)
138147
if (fileName[i] == '.') break;
139-
strcpy_s(fileNameWithoutExtension, MAX_PATH, fileName);
148+
strncpy(fileNameWithoutExtension, fileName, MAX_PATH);
140149
memcpy(fileNameWithoutExtension + i, "\0", 1);
141150
}
142151
};

DexPatcher/config.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef MAX_PATH
2+
#define MAX_PATH 256
3+
#endif
4+
5+
#ifndef IN
6+
#define IN
7+
#endif
8+
9+
// #ifndef OUT
10+
// #define OUT
11+
// #endif

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ releases:https://github.com/FeJQ/DexPatcher/releases/tag/v0.1.0
6464
6565
+ [cmdline](https://github.com/tanakh/cmdline)
6666
67-
+ [nlohmann/json](https://github.com/nlohmann/json)
67+
+ [nlohmann/json](https://github.com/nlohmann/json)
68+
69+
## mac os或linux系统编译
70+
安装cmake编译程序, mac系统可通过brew install cmake安装。
71+
在DexPatcher源码子目录下建一个build子文件夹,cd build 并cmake ../, 然后在make命令即可编译生成对应平台的二进制程序.

0 commit comments

Comments
 (0)