瀏覽代碼

feat: 调整初始化设定

Windows平台设置代码页
Linux平台设置setlocale
SongZihuan 3 年之前
父節點
當前提交
defefeb44e
共有 7 個文件被更改,包括 45 次插入8 次删除
  1. 18 1
      CMakeLists.txt
  2. 6 1
      include/core/core_init.h
  3. 1 1
      include/runtime/aFunlang.h
  4. 10 1
      src/core/core_init.c
  5. 5 1
      src/main.c
  6. 2 2
      src/runtime/aFunlang.c
  7. 3 1
      test/cmake/generic.cmake

+ 18 - 1
CMakeLists.txt

@@ -23,11 +23,23 @@ set(CMAKE_C_VISIBILITY_PRESET "hidden")  # 默认所有符号不可见
 include(CheckCCompilerFlag)
 check_c_compiler_flag("-fPIC" fpic_work)  # 检查是否有fPIC选项
 if (fpic_work)
-    list(APPEND CMAKE_C_FLAGS "-fPIC")
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
 endif()
 
 if (MSVC)
     set(CMAKE_GNUtoMS TRUE)
+
+    check_c_compiler_flag("/utf-8" utf8_work)
+    if (NOT utf8_work)
+        message(FATAL_ERROR "The msvc not support utf-8")
+    endif()
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
+else()
+    check_c_compiler_flag("-fexec-charset=UTF-8" utf8_work)
+    if (NOT utf8_work)
+        message(FATAL_ERROR "The compiler not support utf-8")
+    endif()
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexec-charset=UTF-8")
 endif()
 
 # 相关参数
@@ -59,6 +71,11 @@ set(base_compile_definitions
     aFunMajorVersion=${PROJECT_VERSION_MAJOR}
     aFunMinorVersion=${PROJECT_VERSION_MINOR}
     aFunPatchVersion=${PROJECT_VERSION_PATCH})  # 默认的预定义宏
+
+if (WIN32 OR CYGWIN)
+    list(APPEND base_compile_definitions aFunWIN32=1)
+endif()
+
 add_compile_definitions(${base_compile_definitions})
 
 include(${CMAKE_CURRENT_LIST_DIR}/deps/deps.cmake)  # 安装依赖

+ 6 - 1
include/core/core_init.h

@@ -1,7 +1,12 @@
 #ifndef AFUN_INIT_H
 #define AFUN_INIT_H
 #include "aFunCoreExport.h"
+#include "stdbool.h"
 
-AFUN_CORE_EXPORT void aFunCoreInit(void);
+#ifdef aFunWIN32
+#include "Windows.h"
+#endif
+
+AFUN_CORE_EXPORT bool aFunCoreInit(void);
 
 #endif //AFUN_INIT_H

+ 1 - 1
include/runtime/aFunlang.h

@@ -5,7 +5,7 @@
 
 #include "runtime.h"
 
-AFUN_LANG_EXPORT void aFunInit();
+AFUN_LANG_EXPORT bool aFunInit(void);
 
 AFUN_LANG_EXPORT af_Environment *creatAFunEnviroment(int argc, char **argv);
 AFUN_LANG_EXPORT void destructAFunEnvironment(af_Environment *env);

+ 10 - 1
src/core/core_init.c

@@ -5,7 +5,16 @@
 
 #include "core_init.h"
 #include "tool.h"
+#include <locale.h>
 
-void aFunCoreInit(void) {
+bool aFunCoreInit(void) {
     getEndian();
+    if (setlocale(LC_ALL, "") == NULL)
+        return false;
+#ifdef aFunWIN32
+    if(!SetConsoleOutputCP(65001))  // 设置windows代码页为utf-8编码
+        return false;
+#endif
+    printf("try 中文\n");
+    return true;
 }

+ 5 - 1
src/main.c

@@ -41,7 +41,11 @@ static int mainBuild(ff_FFlags *ff);
 extern const char *help_info;
 
 int main(int argc, char **argv) {
-    aFunInit();
+    if (!aFunInit()) {
+        fprintf(stderr, "aFunlang init error.");
+        return EXIT_FAILURE;
+    }
+
     int exit_code = EXIT_SUCCESS;
     ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, stderr, aFunlang_exe);
     if (ff == NULL)

+ 2 - 2
src/runtime/aFunlang.c

@@ -5,8 +5,8 @@
 static int
 runCode_(FilePath name, af_Parser *parser, int mode, FilePath save_path, FILE *error_file, af_Environment *env);
 
-void aFunInit() {
-    aFunCoreInit();
+bool aFunInit(void) {
+    return aFunCoreInit();
 }
 
 af_Environment *creatAFunEnviroment(int argc, char **argv){

+ 3 - 1
test/cmake/generic.cmake

@@ -1,11 +1,13 @@
 try_compile(generic-test ${CMAKE_BINARY_DIR}/try
             SOURCES ${CMAKE_CURRENT_LIST_DIR}/generic.c
+            OUTPUT_VARIABLE re
             C_STANDARD 11
             C_STANDARD_REQUIRED TRUE
             C_EXTENSIONS FALSE)
 
 if (NOT generic-test)
-    message(FATAL_ERROR "C cannot use _Generic exp.")
+    file(WRITE "${CMAKE_BINARY_DIR}/try.txt" ${re})
+    message(FATAL_ERROR "C cannot use _Generic exp.\n${re}")
 else()
     message(STATUS "C can use _Generic exp.")
 endif()