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

Default variants #144

Open
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions src/tup/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static int top_fd = -1;
int find_tup_dir(void)
{
struct stat st;
static int found_tup_dir = 0;
if (found_tup_dir)
return 0;

if(getcwd(tup_wd, sizeof(tup_wd)) == NULL) {
perror("getcwd");
Expand Down Expand Up @@ -77,6 +80,7 @@ int find_tup_dir(void)
return -1;
}
}
found_tup_dir = 1;
return 0;
}

Expand Down
92 changes: 92 additions & 0 deletions src/tup/hooks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* vim: set ts=8 sw=8 sts=8 noet tw=78:
*
* tup - A file-based build system
*
* Copyright (C) 2011-2012 Mike Shal <marfey@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "hooks.h"
#include "config.h"
#include "option.h"

int run_pre_init_hooks(void)
{
return 0;
}

/* "private" export from tup/main.c */
int variant_command(int argc, char **argv);
static int post_init_variant_hook(void)
{
char argv0[] = "post_init_variants";
char *argv[3];
const char* variants;
char *variants_copy;
char* v;

argv[0] = argv0;
argv[2] = NULL;

variants = tup_option_get_string("post_init.variants");
if (variants == NULL)
return 0;

variants_copy = strdup(variants);
if (variants_copy == NULL) {
fprintf(stderr, "tup error: Failed to allocate memory for variants string\n");
return -1;
}

/* The create_variant command expects find_tup_dir to have
run already, but as a post-init hook we're too early, run it now */
if (find_tup_dir() != 0) {
fprintf(stderr, "tup internal error: Failed to find .tup dir in post-init hook\n");
free(variants_copy);
return -1;
}

v = strtok(variants_copy, " ,");
while (v != NULL) {
argv[1] = v;
if (variant_command(2, argv) != 0) {
fprintf(stderr, "tup error: Post-init hook failed creating variant\n");
fprintf(stderr, "tup error: Failed variant was: %s\n", v);
break;
}
v = strtok(NULL, " ,");
}

free(variants_copy);

if (v == NULL)
return 0;
else
return -1;
}

int run_post_init_hooks(void)
{
printf("Running post-init hooks...\n");

if (post_init_variant_hook() != 0)
return -1;

return 0;
}
27 changes: 27 additions & 0 deletions src/tup/hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* vim: set ts=8 sw=8 sts=8 noet tw=78:
*
* tup - A file-based build system
*
* Copyright (C) 2011-2012 Mike Shal <marfey@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef tup_hooks_h
#define tup_hooks_h

int run_pre_init_hooks(void);
int run_post_init_hooks(void);

#endif
42 changes: 36 additions & 6 deletions src/tup/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "init.h"
#include "config.h"
#include "db.h"
#include "hooks.h"
#include "lock.h"
#include "entry.h"
#include "server.h"
Expand All @@ -40,14 +41,35 @@
#define mkdir(a,b) mkdir(a)
#endif

int tup_init(void)
/* Returns < 0 on error, >= 0 on success. Returns 1 if init_command ran */
int tup_early_init(const char* cmd, int argc, char **argv)
{
int ini_ret;

if (tup_option_init() != 0) {
return -1;
}

tup_temporarily_drop_privs();
ini_ret = tup_option_process_ini(cmd, argc, argv);
tup_restore_privs();

if(ini_ret < 0) {
return ini_ret;
}

if(find_tup_dir() != 0) {
fprintf(stderr, "tup %s usage: tup [args]\n", tup_version());
fprintf(stderr, "For information on Tupfiles and other commands, see the tup(1) man page.\n");
fprintf(stderr, "No .tup directory found. Either create a Tupfile.ini file at the top of your project, or manually run 'tup init' there.\n");
return -1;
}

return ini_ret;
}

int tup_init(void)
{
if(tup_entry_init() < 0) {
return -1;
}
Expand All @@ -63,9 +85,6 @@ int tup_init(void)
if(tup_lock_init() < 0) {
goto out_err;
}
if(tup_option_init() < 0) {
goto out_unlock;
}
color_init();
if(tup_db_open() != 0) {
goto out_unlock;
Expand All @@ -82,7 +101,6 @@ int tup_init(void)
int tup_cleanup(void)
{
tup_db_close();
tup_option_exit();
tup_lock_exit();
if(close(tup_top_fd()) < 0)
perror("close(tup_top_fd())");
Expand Down Expand Up @@ -161,7 +179,7 @@ static int mkdirtree(const char *dirname)
return 0;
}

int init_command(int argc, char **argv)
static int _init_command(int argc, char **argv)
{
int x;
int db_sync = 1;
Expand Down Expand Up @@ -252,3 +270,15 @@ int init_command(int argc, char **argv)
}
return 0;
}

int init_command(int argc, char **argv)
{
if (run_pre_init_hooks() != 0) {
fprintf(stderr, "tup error: pre_init hooks failed\n");
return -1;
}
if (_init_command(argc, argv) != 0) {
return -1;
}
return 0;
}
2 changes: 2 additions & 0 deletions src/tup/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

int tup_early_init(const char* cmd, int argc, char **argv);
void tup_early_cleanup(void);
int tup_init(void);
int tup_cleanup(void);
void tup_valgrind_cleanup(void);
Expand Down
Loading