Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for cross compilation #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMAKE-cross.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER <Path to aarch64-linux-android-gcc>)
SET(CMAKE_CXX_COMPILER <Path to aarch64-linux-android-g++>)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

option(ENABLE_STATIC "Build static (.a) library" ON)

find_package(ZLIB REQUIRED)
option(ENABLE_ZLIB "Build zlip API" ON)

if(ENABLE_ZLIB)
find_package(ZLIB REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_ZLIB_API")
endif(ENABLE_ZLIB)

include_directories(${ZLIB_INCLUDE_DIRS})

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ struct NpyArray {
template<typename T> T* data();
};
```
# Cross Compilation
Use CMAKE-cross.txt as ref to configure your cross tools paths.

Use below command to configure the make
Command: cmake -DCMAKE_TOOLCHAIN_FILE=./CMAKE-cross.txt .

# Zlib:
Turn off ENABLE_ZLIB in CMakeLists.txt to disable ZLIB API - by default it's on.

See [example1.cpp](example1.cpp) for examples of how to use the library. example1 will also be build during cmake installation.
3 changes: 2 additions & 1 deletion cnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ cnpy::NpyArray load_the_npy_file(FILE* fp) {
return arr;
}

#ifdef ENABLE_ZLIB_API
cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncompr_bytes) {

std::vector<unsigned char> buffer_compr(compr_bytes);
Expand Down Expand Up @@ -324,6 +325,7 @@ cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname) {
printf("npz_load: Error! Variable name %s not found in %s!\n",varname.c_str(),fname.c_str());
abort();
}
#endif // ENABLE_ZLIB_API

cnpy::NpyArray cnpy::npy_load(std::string fname) {

Expand All @@ -341,4 +343,3 @@ cnpy::NpyArray cnpy::npy_load(std::string fname) {
}



17 changes: 14 additions & 3 deletions cnpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include<typeinfo>
#include<iostream>
#include<cassert>
#ifdef ENABLE_ZLIB_API
#include<zlib.h>
#endif
#include<map>
#include<memory>
#include<stdint.h>
Expand Down Expand Up @@ -130,6 +132,7 @@ namespace cnpy {
fclose(fp);
}

#ifdef ENABLE_ZLIB_API
template<typename T> void npz_save(std::string zipname, std::string fname, const T* data, const std::vector<size_t>& shape, std::string mode = "w")
{
//first, append a .npy to the fname
Expand Down Expand Up @@ -219,6 +222,7 @@ namespace cnpy {
fwrite(&footer[0],sizeof(char),footer.size(),fp);
fclose(fp);
}
#endif //ENABLE_ZLIB_API

template<typename T> void npy_save(std::string fname, const std::vector<T> data, std::string mode = "w") {
std::vector<size_t> shape;
Expand All @@ -232,18 +236,25 @@ namespace cnpy {
npz_save(zipname, fname, &data[0], shape, mode);
}

template <typename T> std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}

template<typename T> std::vector<char> create_npy_header(const std::vector<size_t>& shape) {

std::vector<char> dict;
dict += "{'descr': '";
dict += BigEndianTest();
dict += map_type(typeid(T));
dict += std::to_string(sizeof(T));
dict += to_string(sizeof(T));
dict += "', 'fortran_order': False, 'shape': (";
dict += std::to_string(shape[0]);
dict += to_string(shape[0]);
for(size_t i = 1;i < shape.size();i++) {
dict += ", ";
dict += std::to_string(shape[i]);
dict += to_string(shape[i]);
}
if(shape.size() == 1) dict += ",";
dict += "), }";
Expand Down
3 changes: 3 additions & 0 deletions example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int main()
//npy array on file now has shape (Nz+Nz,Ny,Nx)
cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"a");

#ifdef ENABLE_ZLIB_API
//now write to an npz file
//non-array variables are treated as 1D arrays with 1 element
double myVar1 = 1.2;
Expand All @@ -52,4 +53,6 @@ int main()
double* mv1 = arr_mv1.data<double>();
assert(arr_mv1.shape.size() == 1 && arr_mv1.shape[0] == 1);
assert(mv1[0] == myVar1);
#endif //ENABLE_ZLIP_API

}