reformat source code, add build rule to check formatting

This commit is contained in:
Ivan Grokhotkov 2018-02-12 13:58:48 +03:00
parent 27bb995464
commit dc797c20bf
4 changed files with 144 additions and 96 deletions

View File

@ -3,13 +3,17 @@ sudo: required
services: services:
- docker - docker
env:
global:
- BUILD_CMD=./build_all_configs.sh
- BUILD_DIR=/workdir
- DOCKER_IMAGE=multiarch/crossbuild
matrix: matrix:
include: include:
- env: - env:
- TARGET_OS=linux64 - TARGET_OS=linux64
- DOCKER_IMAGE=multiarch/crossbuild
- CROSS_TRIPLE=x86_64-linux-gnu - CROSS_TRIPLE=x86_64-linux-gnu
- BUILD_DIR=/workdir
- env: - env:
- TARGET_OS=linux32 - TARGET_OS=linux32
# multiarch/crossbuild doesn't come with 32-bit compilers, use a different image # multiarch/crossbuild doesn't come with 32-bit compilers, use a different image
@ -18,25 +22,18 @@ matrix:
- BUILD_DIR=/work - BUILD_DIR=/work
- env: - env:
- TARGET_OS=linux-armhf - TARGET_OS=linux-armhf
- DOCKER_IMAGE=multiarch/crossbuild
- CROSS_TRIPLE=arm-linux-gnueabihf - CROSS_TRIPLE=arm-linux-gnueabihf
- BUILD_DIR=/workdir - EXTRA_ARGS='-e SKIP_TESTS=1'
- EXTRA_ARGS="-e SKIP_TESTS=1"
- env: - env:
- TARGET_OS=win32 - TARGET_OS=win32
- DOCKER_IMAGE=multiarch/crossbuild
- CROSS_TRIPLE=i686-w64-mingw32 - CROSS_TRIPLE=i686-w64-mingw32
- BUILD_DIR=/workdir
# multiarch/crossbuild doesn't come with 'zip', # multiarch/crossbuild doesn't come with 'zip',
# so we build a tgz archive in the container, and re-package it later in the script. # so we build a tgz archive in the container, and re-package it later in the script.
- EXTRA_ARGS='-e ARCHIVE=tar -e SKIP_TESTS=1' - EXTRA_ARGS='-e ARCHIVE=tar -e SKIP_TESTS=1'
- env: - env:
- TARGET_OS=osx - TARGET_OS=osx
- DOCKER_IMAGE=multiarch/crossbuild
- CROSS_TRIPLE=x86_64-apple-darwin - CROSS_TRIPLE=x86_64-apple-darwin
- BUILD_DIR=/workdir - EXTRA_ARGS='-e SKIP_TESTS=1 -e CXX=c++'
- EXTRA_ARGS="-e SKIP_TESTS=1 -e CXX=c++"
script: script:
- export VER=$(git describe) - export VER=$(git describe)
@ -49,7 +46,7 @@ script:
-e CROSS_TRIPLE=${CROSS_TRIPLE} -e CROSS_TRIPLE=${CROSS_TRIPLE}
${EXTRA_ARGS} ${EXTRA_ARGS}
${DOCKER_IMAGE} ${DOCKER_IMAGE}
./build_all_configs.sh ${BUILD_CMD}
# Diagnostics # Diagnostics
- ls -l - ls -l

View File

@ -66,6 +66,10 @@ OBJ := main.o \
INCLUDES := -Itclap -Iinclude -Ispiffs/src -I. INCLUDES := -Itclap -Iinclude -Ispiffs/src -I.
FILES_TO_FORMAT := $(shell find . -not -path './spiffs/*' \( -name '*.c' -o -name '*.cpp' \))
DIFF_FILES := $(addsuffix .diff,$(FILES_TO_FORMAT))
# clang doesn't seem to handle -D "ARG=\"foo bar\"" correctly, so replace spaces with \x20: # clang doesn't seem to handle -D "ARG=\"foo bar\"" correctly, so replace spaces with \x20:
BUILD_CONFIG_STR := $(shell echo $(CPPFLAGS) | sed 's- -\\\\x20-') BUILD_CONFIG_STR := $(shell echo $(CPPFLAGS) | sed 's- -\\\\x20-')
@ -86,8 +90,6 @@ DIST_NAME := mkspiffs-$(VERSION)$(BUILD_CONFIG_NAME)-$(TARGET_OS)
DIST_DIR := $(DIST_NAME) DIST_DIR := $(DIST_NAME)
DIST_ARCHIVE := $(DIST_NAME).$(ARCHIVE_EXTENSION) DIST_ARCHIVE := $(DIST_NAME).$(ARCHIVE_EXTENSION)
.PHONY: all clean dist
all: $(TARGET) all: $(TARGET)
dist: $(DIST_ARCHIVE) dist: $(DIST_ARCHIVE)
@ -108,7 +110,7 @@ $(DIST_DIR):
@mkdir -p $@ @mkdir -p $@
clean: clean:
@rm -f $(TARGET) $(OBJ) @rm -f $(TARGET) $(OBJ) $(DIFF_FILES)
SPIFFS_TEST_FS_CONFIG := -s 0x100000 -p 512 -b 0x2000 SPIFFS_TEST_FS_CONFIG := -s 0x100000 -p 512 -b 0x2000
@ -132,3 +134,19 @@ test: $(TARGET)
diff spiffs_t spiffs_u diff spiffs_t spiffs_u
rm -f out.{list0,list1,list2,list_u,spiffs_t} rm -f out.{list0,list1,list2,list_u,spiffs_t}
rm -R spiffs_u spiffs_t rm -R spiffs_u spiffs_t
format-check: $(DIFF_FILES)
@rm -f $(DIFF_FILES)
$(DIFF_FILES): %.diff: %
@./format.sh < $< >$<.new
@diff $<.new $< >$@ || ( \
echo "File $^ not formatted correctly. Please use format.sh to re-format it." && \
echo "Here's the diff that caused an error:" && \
echo "" && \
cat $@ && \
rm $@ $<.new && \
exit 1 )
@rm -f $@ $<.new
.PHONY: all clean dist format-check

10
format.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
# Runs astyle with the full set of formatting options
astyle \
--style=otbs \
--indent=spaces=4 \
--convert-tabs \
--keep-one-line-statements \
--pad-header \
--pad-oper \
"$@"

185
main.cpp
View File

@ -53,17 +53,20 @@ static const char* ignored_file_names[] = {
".gitmodules" ".gitmodules"
}; };
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst){ static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
{
memcpy(dst, &s_flashmem[0] + addr, size); memcpy(dst, &s_flashmem[0] + addr, size);
return SPIFFS_OK; return SPIFFS_OK;
} }
static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src){ static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src)
{
memcpy(&s_flashmem[0] + addr, src, size); memcpy(&s_flashmem[0] + addr, src, size);
return SPIFFS_OK; return SPIFFS_OK;
} }
static s32_t api_spiffs_erase(u32_t addr, u32_t size){ static s32_t api_spiffs_erase(u32_t addr, u32_t size)
{
memset(&s_flashmem[0] + addr, 0xff, size); memset(&s_flashmem[0] + addr, 0xff, size);
return SPIFFS_OK; return SPIFFS_OK;
} }
@ -73,7 +76,8 @@ static s32_t api_spiffs_erase(u32_t addr, u32_t size){
//implementation //implementation
int spiffsTryMount(){ int spiffsTryMount()
{
spiffs_config cfg = {0}; spiffs_config cfg = {0};
cfg.phys_addr = 0x0000; cfg.phys_addr = 0x0000;
@ -93,38 +97,45 @@ int spiffsTryMount(){
s_spiffsCache.resize((32 + s_pageSize) * maxOpenFiles); s_spiffsCache.resize((32 + s_pageSize) * maxOpenFiles);
return SPIFFS_mount(&s_fs, &cfg, return SPIFFS_mount(&s_fs, &cfg,
&s_spiffsWorkBuf[0], &s_spiffsWorkBuf[0],
&s_spiffsFds[0], s_spiffsFds.size(), &s_spiffsFds[0], s_spiffsFds.size(),
&s_spiffsCache[0], s_spiffsCache.size(), &s_spiffsCache[0], s_spiffsCache.size(),
NULL); NULL);
} }
bool spiffsMount(){ bool spiffsMount()
if(SPIFFS_mounted(&s_fs)) {
if (SPIFFS_mounted(&s_fs)) {
return true;
}
int res = spiffsTryMount();
if (res != SPIFFS_OK) {
std::cerr << "SPIFFS mount failed with error: " << res << std::endl;
return false;
}
return true; return true;
int res = spiffsTryMount();
if (res != SPIFFS_OK) {
std::cerr << "SPIFFS mount failed with error: " << res << std::endl;
return false;
}
return true;
} }
bool spiffsFormat(){ bool spiffsFormat()
spiffsMount(); {
SPIFFS_unmount(&s_fs); spiffsMount();
int formated = SPIFFS_format(&s_fs);
if(formated != SPIFFS_OK)
return false;
return (spiffsTryMount() == SPIFFS_OK);
}
void spiffsUnmount(){
if(SPIFFS_mounted(&s_fs))
SPIFFS_unmount(&s_fs); SPIFFS_unmount(&s_fs);
int formated = SPIFFS_format(&s_fs);
if (formated != SPIFFS_OK) {
return false;
}
return (spiffsTryMount() == SPIFFS_OK);
} }
int addFile(char* name, const char* path) { void spiffsUnmount()
{
if (SPIFFS_mounted(&s_fs)) {
SPIFFS_unmount(&s_fs);
}
}
int addFile(char* name, const char* path)
{
FILE* src = fopen(path, "rb"); FILE* src = fopen(path, "rb");
if (!src) { if (!src) {
std::cerr << "error: failed to open " << path << " for reading" << std::endl; std::cerr << "error: failed to open " << path << " for reading" << std::endl;
@ -144,7 +155,7 @@ int addFile(char* name, const char* path) {
size_t left = size; size_t left = size;
uint8_t data_byte; uint8_t data_byte;
while (left > 0){ while (left > 0) {
if (1 != fread(&data_byte, 1, 1, src)) { if (1 != fread(&data_byte, 1, 1, src)) {
std::cerr << "fread error!" << std::endl; std::cerr << "fread error!" << std::endl;
@ -180,7 +191,8 @@ int addFile(char* name, const char* path) {
return 0; return 0;
} }
int addFiles(const char* dirname, const char* subPath) { int addFiles(const char* dirname, const char* subPath)
{
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
bool error = false; bool error = false;
@ -226,15 +238,12 @@ int addFiles(const char* dirname, const char* subPath) {
newSubPath += ent->d_name; newSubPath += ent->d_name;
newSubPath += "/"; newSubPath += "/";
if (addFiles(dirname, newSubPath.c_str()) != 0) if (addFiles(dirname, newSubPath.c_str()) != 0) {
{
std::cerr << "Error for adding content from " << ent->d_name << "!" << std::endl; std::cerr << "Error for adding content from " << ent->d_name << "!" << std::endl;
} }
continue; continue;
} } else {
else
{
std::cerr << "skipping " << ent->d_name << std::endl; std::cerr << "skipping " << ent->d_name << std::endl;
continue; continue;
} }
@ -264,7 +273,8 @@ int addFiles(const char* dirname, const char* subPath) {
return (error) ? 1 : 0; return (error) ? 1 : 0;
} }
void listFiles() { void listFiles()
{
spiffs_DIR dir; spiffs_DIR dir;
spiffs_dirent ent; spiffs_dirent ent;
@ -272,8 +282,9 @@ void listFiles() {
spiffs_dirent* it; spiffs_dirent* it;
while (true) { while (true) {
it = SPIFFS_readdir(&dir, &ent); it = SPIFFS_readdir(&dir, &ent);
if (!it) if (!it) {
break; break;
}
std::cout << it->size << '\t' << it->name << std::endl; std::cout << it->size << '\t' << it->name << std::endl;
} }
@ -287,7 +298,8 @@ void listFiles() {
* *
* @author Pascal Gollor (http://www.pgollor.de/cms/) * @author Pascal Gollor (http://www.pgollor.de/cms/)
*/ */
bool dirExists(const char* path) { bool dirExists(const char* path)
{
DIR *d = opendir(path); DIR *d = opendir(path);
if (d) { if (d) {
@ -305,10 +317,11 @@ bool dirExists(const char* path) {
* *
* @author Pascal Gollor (http://www.pgollor.de/cms/) * @author Pascal Gollor (http://www.pgollor.de/cms/)
*/ */
bool dirCreate(const char* path) { bool dirCreate(const char* path)
{
// Check if directory also exists. // Check if directory also exists.
if (dirExists(path)) { if (dirExists(path)) {
return false; return false;
} }
// platform stuff... // platform stuff...
@ -317,8 +330,8 @@ bool dirCreate(const char* path) {
#else #else
if (mkdir(path, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) { if (mkdir(path, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
#endif #endif
std::cerr << "Can not create directory!!!" << std::endl; std::cerr << "Can not create directory!!!" << std::endl;
return false; return false;
} }
return true; return true;
@ -332,7 +345,8 @@ bool dirCreate(const char* path) {
* *
* @author Pascal Gollor (http://www.pgollor.de/cms/) * @author Pascal Gollor (http://www.pgollor.de/cms/)
*/ */
bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath) { bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath)
{
u8_t buffer[spiffsFile->size]; u8_t buffer[spiffsFile->size];
std::string filename = (const char*)(spiffsFile->name); std::string filename = (const char*)(spiffsFile->name);
@ -367,7 +381,8 @@ bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath) {
* *
* todo: Do unpack stuff for directories. * todo: Do unpack stuff for directories.
*/ */
bool unpackFiles(std::string sDest) { bool unpackFiles(std::string sDest)
{
spiffs_DIR dir; spiffs_DIR dir;
spiffs_dirent ent; spiffs_dirent ent;
@ -422,12 +437,12 @@ bool unpackFiles(std::string sDest) {
// Output stuff. // Output stuff.
std::cout std::cout
<< it->name << it->name
<< '\t' << '\t'
<< " > " << sDestFilePath << " > " << sDestFilePath
<< '\t' << '\t'
<< "size: " << it->size << " Bytes" << "size: " << it->size << " Bytes"
<< std::endl; << std::endl;
} }
// Get next file handle. // Get next file handle.
@ -442,7 +457,8 @@ bool unpackFiles(std::string sDest) {
// Actions // Actions
int actionPack() { int actionPack()
{
if (!dirExists(s_dirName.c_str())) { if (!dirExists(s_dirName.c_str())) {
std::cerr << "error: can't read source directory" << std::endl; std::cerr << "error: can't read source directory" << std::endl;
return 1; return 1;
@ -451,7 +467,7 @@ int actionPack() {
if (s_imageSize == 0) { if (s_imageSize == 0) {
s_imageSize = 0x10000; s_imageSize = 0x10000;
} }
s_flashmem.resize(s_imageSize, 0xff); s_flashmem.resize(s_imageSize, 0xff);
FILE* fdres = fopen(s_imageName.c_str(), "wb"); FILE* fdres = fopen(s_imageName.c_str(), "wb");
@ -464,7 +480,7 @@ int actionPack() {
int result = addFiles(s_dirName.c_str(), "/"); int result = addFiles(s_dirName.c_str(), "/");
spiffsUnmount(); spiffsUnmount();
fwrite(&s_flashmem[0], 4, s_flashmem.size()/4, fdres); fwrite(&s_flashmem[0], 4, s_flashmem.size() / 4, fdres);
fclose(fdres); fclose(fdres);
return result; return result;
@ -484,7 +500,8 @@ size_t getFileSize(FILE* fp)
* *
* @author Pascal Gollor (http://www.pgollor.de/cms/) * @author Pascal Gollor (http://www.pgollor.de/cms/)
*/ */
int actionUnpack(void) { int actionUnpack(void)
{
int ret = 0; int ret = 0;
// open spiffs image // open spiffs image
@ -501,7 +518,7 @@ int actionUnpack(void) {
s_flashmem.resize(s_imageSize, 0xff); s_flashmem.resize(s_imageSize, 0xff);
// read content into s_flashmem // read content into s_flashmem
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc); fread(&s_flashmem[0], 4, s_flashmem.size() / 4, fdsrc);
// close file handle // close file handle
fclose(fdsrc); fclose(fdsrc);
@ -524,7 +541,8 @@ int actionUnpack(void) {
} }
int actionList() { int actionList()
{
FILE* fdsrc = fopen(s_imageName.c_str(), "rb"); FILE* fdsrc = fopen(s_imageName.c_str(), "rb");
if (!fdsrc) { if (!fdsrc) {
std::cerr << "error: failed to open image file" << std::endl; std::cerr << "error: failed to open image file" << std::endl;
@ -537,7 +555,7 @@ int actionList() {
s_flashmem.resize(s_imageSize, 0xff); s_flashmem.resize(s_imageSize, 0xff);
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc); fread(&s_flashmem[0], 4, s_flashmem.size() / 4, fdsrc);
fclose(fdsrc); fclose(fdsrc);
if (!spiffsMount()) { if (!spiffsMount()) {
@ -550,7 +568,8 @@ int actionList() {
return 0; return 0;
} }
int actionVisualize() { int actionVisualize()
{
FILE* fdsrc = fopen(s_imageName.c_str(), "rb"); FILE* fdsrc = fopen(s_imageName.c_str(), "rb");
if (!fdsrc) { if (!fdsrc) {
std::cerr << "error: failed to open image file" << std::endl; std::cerr << "error: failed to open image file" << std::endl;
@ -564,7 +583,7 @@ int actionVisualize() {
s_flashmem.resize(s_imageSize, 0xff); s_flashmem.resize(s_imageSize, 0xff);
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc); fread(&s_flashmem[0], 4, s_flashmem.size() / 4, fdsrc);
fclose(fdsrc); fclose(fdsrc);
if (!spiffsMount()) { if (!spiffsMount()) {
@ -586,30 +605,33 @@ int actionVisualize() {
class CustomOutput : public TCLAP::StdOutput class CustomOutput : public TCLAP::StdOutput
{ {
public: public:
virtual void version(TCLAP::CmdLineInterface& c) virtual void version(TCLAP::CmdLineInterface& c)
{ {
std::cout << "mkspiffs ver. " VERSION << std::endl; std::cout << "mkspiffs ver. " VERSION << std::endl;
const char* configName = BUILD_CONFIG_NAME; const char* configName = BUILD_CONFIG_NAME;
if (configName[0] == '-') configName += 1; if (configName[0] == '-') {
std::cout << "Build configuration name: " << configName << std::endl; configName += 1;
std::cout << "SPIFFS ver. " SPIFFS_VERSION << std::endl;
const char* buildConfig = BUILD_CONFIG;
std::cout << "Extra build flags: " << (strlen(buildConfig) ? buildConfig : "(none)") << std::endl;
std::cout << "SPIFFS configuration:" << std::endl;
PRINT_INT_MACRO(SPIFFS_OBJ_NAME_LEN);
PRINT_INT_MACRO(SPIFFS_OBJ_META_LEN);
PRINT_INT_MACRO(SPIFFS_USE_MAGIC);
#if SPIFFS_USE_MAGIC == 1
PRINT_INT_MACRO(SPIFFS_USE_MAGIC_LENGTH);
#endif
PRINT_INT_MACRO(SPIFFS_ALIGNED_OBJECT_INDEX_TABLES);
} }
std::cout << "Build configuration name: " << configName << std::endl;
std::cout << "SPIFFS ver. " SPIFFS_VERSION << std::endl;
const char* buildConfig = BUILD_CONFIG;
std::cout << "Extra build flags: " << (strlen(buildConfig) ? buildConfig : "(none)") << std::endl;
std::cout << "SPIFFS configuration:" << std::endl;
PRINT_INT_MACRO(SPIFFS_OBJ_NAME_LEN);
PRINT_INT_MACRO(SPIFFS_OBJ_META_LEN);
PRINT_INT_MACRO(SPIFFS_USE_MAGIC);
#if SPIFFS_USE_MAGIC == 1
PRINT_INT_MACRO(SPIFFS_USE_MAGIC_LENGTH);
#endif
PRINT_INT_MACRO(SPIFFS_ALIGNED_OBJECT_INDEX_TABLES);
}
}; };
#undef PRINT_INT_MACRO #undef PRINT_INT_MACRO
void processArgs(int argc, const char** argv) { void processArgs(int argc, const char** argv)
{
TCLAP::CmdLine cmd("", ' ', VERSION); TCLAP::CmdLine cmd("", ' ', VERSION);
CustomOutput output; CustomOutput output;
cmd.setOutput(&output); cmd.setOutput(&output);
@ -659,11 +681,12 @@ void processArgs(int argc, const char** argv) {
s_addAllFiles = addAllFilesArg.isSet(); s_addAllFiles = addAllFilesArg.isSet();
} }
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[])
{
try { try {
processArgs(argc, argv); processArgs(argc, argv);
} catch(...) { } catch (...) {
std::cerr << "Invalid arguments" << std::endl; std::cerr << "Invalid arguments" << std::endl;
return 1; return 1;
} }
@ -673,7 +696,7 @@ int main(int argc, const char * argv[]) {
return actionPack(); return actionPack();
break; break;
case ACTION_UNPACK: case ACTION_UNPACK:
return actionUnpack(); return actionUnpack();
break; break;
case ACTION_LIST: case ACTION_LIST:
return actionList(); return actionList();